Last active
September 12, 2018 08:33
-
-
Save jeffguorg/250f2b37971a1772969fef12b93324b0 to your computer and use it in GitHub Desktop.
an asyncio daemon pattern
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
import asyncio | |
import queue | |
ioloop = asyncio.get_event_loop() | |
def timer(interval=5): | |
def wrapper(func): | |
async def runner(*args, **kwargs): | |
while True: | |
asyncio.ensure_future(func(*args, **kwargs)) | |
await asyncio.sleep(interval) | |
return runner | |
return wrapper | |
class ConcurrencyExceededException(Exception): | |
pass | |
def locker(quiet=True, concurrency=1): | |
assert concurrency > 0 | |
locker_queue = queue.Queue(maxsize=concurrency) | |
def wrapper(func): | |
async def runner(*args, **kwargs): | |
try: | |
locker_queue.put(object(), block=False) | |
except queue.Full: | |
if quiet: | |
return | |
else: | |
raise ConcurrencyExceededException() | |
try: | |
return await func(*args, **kwargs) | |
finally: | |
locker_queue.get() | |
return runner | |
return wrapper | |
@timer(1) | |
@locker(quiet=False, concurrency=2) | |
async def test(): | |
await asyncio.sleep(5) | |
print("hey") | |
ioloop.run_until_complete(test()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment