-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Asyncio support #1251
Comments
You can achieve this by spawning greenlets within your locust tasks. Here's a small example: from gevent.pool import Pool
from locust import HttpLocust, TaskSet, task, constant
class MyLocust(HttpLocust):
host = "https://docs.locust.io"
wait_time = constant(5)
class task_set(TaskSet):
@task
def dual_greenlet_task(self):
def do_http_request_or_whatever():
print("yay, running in separate greenlet")
response = self.client.get("/")
print("status code:", response.status_code)
pool = Pool()
pool.spawn(do_http_request_or_whatever)
pool.spawn(do_http_request_or_whatever)
pool.join() Locust is heavily reliant on Gevent, and as far as I know gevent and python async are not 100% compatible. Therefore I don't see locust supporting python |
Alright thanks for the explanation, example and link 🙂 |
Is there any reasons to not switch to asyncio completely? |
The main reason is that it would be a ton of work to rewrite everything. Apart from that I think it is a great idea :) |
AFAIK using different threads to make many parallel requests with |
From the description of that issue it sounds like it should only be a problem if you're using the same client to connect to many different hosts. In that case one could create another |
@heyman in my case it happens when I'm executing many requests on the same host (as in your example). |
@heyman sorry about confusion, looks like it was my mistake. Everything is ok with |
Do we support Asyncio in locust task now? |
hey guys, maintainers, theoretically, do you see any potential issues with using asgiref project to execute async functions as sync ones ? |
Havent tried it, would love to see a proof of concept! |
Most asyncio wrappers have issues coexisting with gevent though.. but if it works then it would be awesome! |
gevent now has an asyncio version, so that should make integration easier: |
Unfortunately that repo hasnt been updated in two years, so I'm not sure if it works any more. I think it would be doable to implement an asyncio version of the locust worker, but it'll take some work. |
Is there a more concrete example of calling ayncio coroutine inside a Locust gevent task? Sorry but it's not immediately obvious after reading the code examples above (where I can't find where asyncio coroutines are called...) |
Asyncio is not supported right now. We're planning to build a completely asyncio-based worker implementation, but unless theres a simple way to allow gevent and asyncio to coexist (like asyncio-gevent seemed to promise before it went silent) it might be a while. In the event that someone wants to take it upon themselves to reimplement the worker on asyncio themselves, it would be most welcome :) |
Thanks @cyberw for your prompt reply. After days of struggle, I finally found a way to make asyncio work in Locust. Here is a working example. The basic idea is to create a dedicated thread with the asyncio event loop in each process (since Locust can start multiple workers on multiple CPU cores that run in separate processes), and define a lambda function that takes a coroutine and its parameters as the argument, and dynamically run it in the event loop in the process that it belongs.
|
Follow up of #924 and #1079.
Is your feature request related to a problem? Please describe.
Sometimes one needs to simulate an asynchronous user behavior. Hatching more users does not solve the problem.
Describe the solution you'd like
Support defining
async
tasks or something similar.Describe alternatives you've considered
One could also write a custom client. Maybe this would solve the problem. It would be great to collect examples or snippets on how to do so in this issue, and add them in https://docs.locust.io/en/stable/testing-other-systems.html or another doc page.
The text was updated successfully, but these errors were encountered: