-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
An idea for implementing aio. I dont know if this should/could be com…
…bined with gevent or if we should have pure async workers. Things left to do are of course: * Implement request events (subclass ClientSession) * Implement communication with master & User management (duh)
- Loading branch information
Showing
3 changed files
with
605 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import asyncio | ||
|
||
import aiohttp | ||
|
||
|
||
class AIOUser: | ||
tasks = [] | ||
|
||
def run(self) -> None: | ||
self._running = True | ||
self._task: asyncio.Task = asyncio.create_task(self._run_tasks()) | ||
|
||
async def _run_tasks(self) -> None: | ||
try: | ||
while self._running: | ||
for t in self.tasks: | ||
await t(self) | ||
except asyncio.CancelledError: | ||
print("cancelled") | ||
finally: | ||
print("stopped") | ||
|
||
async def stop(self) -> None: | ||
self._running = False | ||
self._task.cancel() | ||
|
||
|
||
class AIOHttpUser(AIOUser): | ||
def __init__(self) -> None: | ||
self.client = aiohttp.ClientSession() | ||
|
||
async def stop(self) -> None: | ||
await super().stop() | ||
await self.client.close() | ||
|
||
|
||
class MyUser(AIOHttpUser): | ||
async def task_1(self) -> None: | ||
resp = await self.client.get("https://www.locust.cloud") | ||
print(resp) | ||
|
||
async def task_2(self) -> None: | ||
print(await self.client.get("https://www.google.com")) | ||
|
||
tasks = [task_1, task_2] | ||
|
||
|
||
async def main() -> None: | ||
a = MyUser() | ||
b = MyUser() | ||
a.run() | ||
b.run() | ||
await asyncio.sleep(5) | ||
await a.stop() | ||
await b.stop() | ||
|
||
|
||
asyncio.run(main()) |
Oops, something went wrong.