python-asyncio

Handling Timeouts with asyncio

我与影子孤独终老i 提交于 2020-04-11 10:07:45
问题 Disclaimer: this is my first time experimenting with the asyncio module. I'm using asyncio.wait in the following manner to try to support a timeout feature waiting for all results from a set of async tasks. This is part of a larger library so I'm omitting some irrelevant code. Note that the library already supports submitting tasks and using timeouts with ThreadPoolExecutors and ProcessPoolExecutors, so I'm not really interested in suggestions to use those instead or questions about why I'm

Handling Timeouts with asyncio

戏子无情 提交于 2020-04-11 10:07:01
问题 Disclaimer: this is my first time experimenting with the asyncio module. I'm using asyncio.wait in the following manner to try to support a timeout feature waiting for all results from a set of async tasks. This is part of a larger library so I'm omitting some irrelevant code. Note that the library already supports submitting tasks and using timeouts with ThreadPoolExecutors and ProcessPoolExecutors, so I'm not really interested in suggestions to use those instead or questions about why I'm

Python - How to cancel a specific task spawned by a nursery in python-trio

痞子三分冷 提交于 2020-04-07 05:26:19
问题 I have an async function that listens on a specific port. I want to run the function on a few ports at a time and when the user wants to stop listening on a specific port, stop the function listening on that port. Previously I was using the asyncio library for this task and I tackled this problem by creating tasks with a unique id as their name. asyncio.create_task(Func(), name=UNIQUE_ID) Since trio uses nurseries to spawn tasks, I can see the running tasks by using nursery.child_tasks but

When asyncio task gets stored after creation, exceptions from task get muted

∥☆過路亽.° 提交于 2020-04-04 06:30:50
问题 I was using asyncio for a project, and encountered this strange behavior. import asyncio def schedule_something(): global f tsk = asyncio.async(do_something()) f = tsk #If this line is commented out, exceptions can be heard. @asyncio.coroutine def do_something(): raise Exception() loop = asyncio.get_event_loop() loop.call_soon(schedule_something) loop.run_forever() loop.close() For some reason, storing the resulting task when you call asyncio.async() stops exceptions from doing anything.

How to update global variable in asyncio create_task

给你一囗甜甜゛ 提交于 2020-03-25 18:22:41
问题 I currently have a global variable that's not being set across the application. I have two files where file2 imports from file1. The global is intialized in file1. Here is the code that initializes the global variable and later uses it in file1. import time import asyncio #Initialize global CONNECTION_OPEN = False async def calculate_idle(t): orig_time = t global CONNECTION_OPEN while True: await asyncio.sleep(5) print("GLOBAL CONNECTION", CONNECTION_OPEN) if CONNECTION_OPEN: print("This

How i call async function without await?

久未见 提交于 2020-03-14 07:48:03
问题 I have a controller action in aiohttp application. async def handler_message(request): try: content = await request.json() perform_message(x,y,z) except (RuntimeError): print("error in perform fb message") finally: return web.Response(text="Done") perform_message is async function. Now, when I call action I want that my action return as soon as possible and perform_message put in event loop. In this way, perform_message isn't executed 回答1: One way would be to use create_task function: import

How i call async function without await?

≯℡__Kan透↙ 提交于 2020-03-14 07:46:54
问题 I have a controller action in aiohttp application. async def handler_message(request): try: content = await request.json() perform_message(x,y,z) except (RuntimeError): print("error in perform fb message") finally: return web.Response(text="Done") perform_message is async function. Now, when I call action I want that my action return as soon as possible and perform_message put in event loop. In this way, perform_message isn't executed 回答1: One way would be to use create_task function: import

Starlette + asyncio.create_task() doesn't log error if task object is stored in instance variable [duplicate]

社会主义新天地 提交于 2020-03-04 18:43:23
问题 This question already has answers here : When asyncio task gets stored after creation, exceptions from task get muted (2 answers) Closed 13 days ago . Okay, this is very weird, but here goes - import asyncio from starlette.applications import Starlette class MyTasks: def __init__(self): self.task = None async def main(self): self.task = asyncio.create_task(self.hello()) async def hello(self): raise ValueError async def main(): await MyTasks().main() app = Starlette(on_startup=[main]) $

Is it possible to limit the number of coroutines running corcurrently in asyncio?

爱⌒轻易说出口 提交于 2020-02-20 06:39:26
问题 I already wrote my script using asyncio but found that the number of coroutines running simultaneously is too large and it often ends up hanging around. So I would like to limit the number of coroutines concurrently, and once it reaches the limit, I want to wait for any coroutine to be finished before another is executed. My current code is something like the following: loop = asyncio.get_event_loop() p = map(my_func, players) result = loop.run_until_complete(asyncio.gather(*p)) async def my

Is it possible to limit the number of coroutines running corcurrently in asyncio?

◇◆丶佛笑我妖孽 提交于 2020-02-20 06:39:11
问题 I already wrote my script using asyncio but found that the number of coroutines running simultaneously is too large and it often ends up hanging around. So I would like to limit the number of coroutines concurrently, and once it reaches the limit, I want to wait for any coroutine to be finished before another is executed. My current code is something like the following: loop = asyncio.get_event_loop() p = map(my_func, players) result = loop.run_until_complete(asyncio.gather(*p)) async def my