Skip to content

Commit

Permalink
An idea for implementing aio. I dont know if this should/could be com…
Browse files Browse the repository at this point in the history
…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
cyberw committed Dec 18, 2024
1 parent 961123f commit 443925a
Show file tree
Hide file tree
Showing 3 changed files with 605 additions and 1 deletion.
58 changes: 58 additions & 0 deletions async.py
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())
Loading

0 comments on commit 443925a

Please sign in to comment.