python-asyncio

Difference between `asyncio.wait([asyncio.sleep(5)])` and `asyncio.sleep(5)`

非 Y 不嫁゛ 提交于 2021-02-15 07:33:49
问题 Could somebody please explain why there is a 5 second delay between coro2 finishing and coro1 finishing? Also, why is there no such delay if I replace asyncio.wait([asyncio.sleep(5)]) with asyncio.sleep(5) ? async def coro1(): logger.info("coro1 start") await asyncio.wait([asyncio.sleep(5)]) logger.info("coro1 finish") async def coro2(): logger.info("coro2 start") time.sleep(10) logger.info("coro2 finish") async def main(): await asyncio.gather(coro1(), coro2()) loop = asyncio.get_event_loop(

Asyncio: Weirdness of Task exception was never retrieved

杀马特。学长 韩版系。学妹 提交于 2021-02-15 03:51:36
问题 Lets assume I have a simple code: import asyncio async def exc(): print(1 / 0) loop = asyncio.get_event_loop() loop.create_task(exc()) try: loop.run_forever() except KeyboardInterrupt: loop.stop() loop.close() If I run it, I get error message immediately Task exception was never retrieved future: <Task finished coro=<exc() done, defined at qq.py:4> exception=ZeroDivisionError('division by zero',)> Traceback (most recent call last): File "qq.py", line 5, in exc print(1 / 0) ZeroDivisionError:

Restful to serve a websocket - architecture with a single thread and asyncio?

倖福魔咒の 提交于 2021-02-11 17:01:23
问题 I have a websocket that I access from python as below. I then want to expose this websocket (with slightly altered data) over a restful interface (it cannot be a websocket for my purposes), where a client can ping it, and should get the latest fully up to date information (each time the full dataset from the beginning). How can I avoid that the websocket from restarting each time the client pings the restful interface? The code looks as follows: websocket: class WebSocketExample: async def

How to add a FileTransport cleanly to Asyncio?

蹲街弑〆低调 提交于 2021-02-11 15:48:49
问题 I'm writing an application which reads text data and acts on it. The text data could come from a TCP port, or from a text file (which contains data earlier read from the TCP port and archived). I'm writing it in Python 3, and using asyncio seems like the obvious tool to use. It's straightforward to use the Streams API open_connection() to open the TCP port and read from it. The asyncio architecture has the concept a Transport and a Protocol for the lower and upper layers of input-output. So,

How to add a FileTransport cleanly to Asyncio?

依然范特西╮ 提交于 2021-02-11 15:47:21
问题 I'm writing an application which reads text data and acts on it. The text data could come from a TCP port, or from a text file (which contains data earlier read from the TCP port and archived). I'm writing it in Python 3, and using asyncio seems like the obvious tool to use. It's straightforward to use the Streams API open_connection() to open the TCP port and read from it. The asyncio architecture has the concept a Transport and a Protocol for the lower and upper layers of input-output. So,

Sending and receiving frames over the same websocket connection without blocking

你。 提交于 2021-02-11 12:46:13
问题 Sorry for the long post but I've been poking at this for over a week so I've tried a lot of different stuff. I know Python well enough but I don't have any experience with asyncio or non-blocking functions in Python. I'm writing an API library/module/package/whatever for a web service that requires a websocket connection. There are many incoming messages to act on, and some control-related (web app level, not websocket control messages) that I need to send on occasion. I can easily receive

Sending and receiving frames over the same websocket connection without blocking

删除回忆录丶 提交于 2021-02-11 12:45:54
问题 Sorry for the long post but I've been poking at this for over a week so I've tried a lot of different stuff. I know Python well enough but I don't have any experience with asyncio or non-blocking functions in Python. I'm writing an API library/module/package/whatever for a web service that requires a websocket connection. There are many incoming messages to act on, and some control-related (web app level, not websocket control messages) that I need to send on occasion. I can easily receive

Python's asyncio.Event() across different classes

断了今生、忘了曾经 提交于 2021-02-11 07:17:48
问题 I'm writing a Python program to interact with a device based on a CAN Bus. I'm using the python-can module successfully for this purpose. I'm also using asyncio to react to asynchronous events. I have written a "CanBusManager" class that is used by the "CanBusSequencer" class. The "CanBusManager" class takes care of generating/sending/receiving messages, and the CanBusSequencer drives the sequence of messages to be sent. At some point in the sequence I want to wait until a specific message is

Python's asyncio.Event() across different classes

怎甘沉沦 提交于 2021-02-11 07:10:21
问题 I'm writing a Python program to interact with a device based on a CAN Bus. I'm using the python-can module successfully for this purpose. I'm also using asyncio to react to asynchronous events. I have written a "CanBusManager" class that is used by the "CanBusSequencer" class. The "CanBusManager" class takes care of generating/sending/receiving messages, and the CanBusSequencer drives the sequence of messages to be sent. At some point in the sequence I want to wait until a specific message is

Python's asyncio.Event() across different classes

大憨熊 提交于 2021-02-11 07:10:03
问题 I'm writing a Python program to interact with a device based on a CAN Bus. I'm using the python-can module successfully for this purpose. I'm also using asyncio to react to asynchronous events. I have written a "CanBusManager" class that is used by the "CanBusSequencer" class. The "CanBusManager" class takes care of generating/sending/receiving messages, and the CanBusSequencer drives the sequence of messages to be sent. At some point in the sequence I want to wait until a specific message is