Last active
May 17, 2018 05:19
-
-
Save suryadana/76030f6b4c9b029c497e1cc71b40f32f to your computer and use it in GitHub Desktop.
I have stupid decorator for running your function with coroutine asyncio. Don't expect for the script, because it is script kiddie.
This file contains hidden or 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 | |
def static_async(method): | |
def wrapper(*args, **kwargs): | |
event = asyncio.new_event_loop() | |
asyncio.set_event_loop(event) | |
event.run_until_complete(method(*args, **kwargs)) | |
event.close() | |
return wrapper | |
def dynamic_async(method): | |
def wrapper(*args, **kwargs): | |
event = asyncio.new_event_loop() | |
asyncio.set_event_loop(event) | |
taks = (method(*args, **kwargs),) | |
result = event.run_until_complete(asyncio.gather(*taks)) | |
event.close() | |
return result[0] | |
return wrapper | |
# Example | |
@static_async | |
async def example_static(): | |
for i in range(0, 10): | |
print('Are you stupid?') | |
@dynamic_async | |
async def example_dynamic(x, y): | |
return x * y | |
example_static() | |
example_dynamic(1, 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment