structio/tests/smart_events.py

26 lines
891 B
Python

import structio
from functools import partial
@structio.on_event("on_message")
async def test(evt, *args, **kwargs):
print(f"[test] New event {evt!r} with arguments: {args}, {kwargs}")
# Simulate some work
await structio.sleep(1)
async def main():
print("[main] Firing two events synchronously")
t = structio.clock()
await structio.emit("on_message", 1, 2, 3, **{"foo": "bar"})
await structio.emit("on_message", 1, 2, 4, **{"foo": "baz"})
print(f"[main] Done in {structio.clock() - t:.2f} seconds. Firing two events in parallel")
t = structio.clock()
async with structio.create_pool() as pool:
pool.spawn(partial(structio.emit, "on_message", 1, 2, 3, **{"foo": "bar"}))
pool.spawn(partial(structio.emit, "on_message", 1, 2, 4, **{"foo": "baz"}))
print(f"[main] Done in {structio.clock() - t:.2f} seconds")
structio.run(main)