python-asyncio

How to shutdown the loop and print error if coroutine raised an exception with asyncio?

坚强是说给别人听的谎言 提交于 2020-06-10 02:32:26
问题 Suppose I have a few coroutines running in a loop. How to make so that if some of them failed with exception the whole program would fail with this exception? Because right now asyncio doesn't even prints the error messages from coroutines unless I use logging level "DEBUG". from asyncio import get_event_loop, sleep async def c(sleep_time=2, fail=False): print('c', sleep_time, fail) if fail: raise Exception('fail') while True: print('doing stuff') await sleep(sleep_time) loop = get_event_loop

Why can't I 'yield from' inside an async function?

不羁的心 提交于 2020-06-10 02:19:49
问题 In Python 3.6, I am able to use yield inside a coroutine. However I am not able to use yield from . Below is my code. On line 3 I await another coroutine. On line 4 I try to yield from a file. Why won't Python 3.6 allow me to do that? async def read_file(self, filename): with tempfile.NamedTemporaryFile(mode='r', delete=True, dir='/tmp', prefix='sftp') as tmp_file: await self.copy_file(filename, tmp_file) yield from open(tmp_file) Here's the exception Python 3.6 raises for the above code:

Why can't I 'yield from' inside an async function?

十年热恋 提交于 2020-06-10 02:19:36
问题 In Python 3.6, I am able to use yield inside a coroutine. However I am not able to use yield from . Below is my code. On line 3 I await another coroutine. On line 4 I try to yield from a file. Why won't Python 3.6 allow me to do that? async def read_file(self, filename): with tempfile.NamedTemporaryFile(mode='r', delete=True, dir='/tmp', prefix='sftp') as tmp_file: await self.copy_file(filename, tmp_file) yield from open(tmp_file) Here's the exception Python 3.6 raises for the above code:

Receiving streaming data after implementing asyncio websockets as a class?

|▌冷眼眸甩不掉的悲伤 提交于 2020-06-08 13:18:19
问题 My question is closely related to the following question on Stackoverflow and the documentation here. I am defining a websockets -connection as a class. Next, I create a new class where I call the earlier defined websocket-class as self.ws and tell which data to send to the websocket with self.request . My problem is that the current script only runs once, whereas my desired output is continuous data. The second link shows that I can retrieve continuous / streaming data using asyncio.get

Write back through the callback attached to IOLoop in Tornado

白昼怎懂夜的黑 提交于 2020-06-01 07:13:31
问题 There is a tricky post handler, sometimes it can take a lots of time (depending on a input values), sometimes not. What I want is to write back whenever 1 second passes, dynamically allocating the response. def post(): def callback(): self.write('too-late') self.finish() timeout_obj = IOLoop.current().add_timeout( dt.timedelta(seconds=1), callback, ) # some asynchronous operations if not self.request.connection.stream.closed(): self.write('here is your response') self.finish() IOLoop.current(

Write back through the callback attached to IOLoop in Tornado

拜拜、爱过 提交于 2020-06-01 07:13:27
问题 There is a tricky post handler, sometimes it can take a lots of time (depending on a input values), sometimes not. What I want is to write back whenever 1 second passes, dynamically allocating the response. def post(): def callback(): self.write('too-late') self.finish() timeout_obj = IOLoop.current().add_timeout( dt.timedelta(seconds=1), callback, ) # some asynchronous operations if not self.request.connection.stream.closed(): self.write('here is your response') self.finish() IOLoop.current(

RuntimeError: Timeout context manager should be used inside a task

◇◆丶佛笑我妖孽 提交于 2020-05-29 03:19:05
问题 Background: I am hosting a flask server alongside a discord client The flask server just needs to pass on messages from the client to discord and from messages from discord to the client. I am getting the error when I call loop.run_until_complete(sendMsg(request)) I have tried wait_for in sendMsg and wait_for loop.run_until_complete() I have looked everywhere and haven't found anything so any help would be appreciated. Code: import discord import json import os import asyncio from flask

Python create_task does not work in running event loop

こ雲淡風輕ζ 提交于 2020-05-26 09:46:20
问题 I have a simple piece of code driving me crazy for a while. I have posted this question some days ago asking create_task is not working with input . Now I have figured out something related to this. I am running event loop in a separate thread and pushing tasks in it. Very straight forward code. import asyncio import threading async def printer(message): print(f'[printer] {message}') def loop_runner(loop): loop.run_forever() if __name__ == '__main__': event_loop = asyncio.get_event_loop() t =

What are all these deprecated “loop” parameters in asyncio?

北慕城南 提交于 2020-05-25 07:11:38
问题 A lot of the functions in asyncio have deprecated loop parameters, scheduled to be removed in Python 3.10. Examples include as_completed(), sleep(), and wait(). I'm looking for some historical context on these parameters and their removal. What problems did loop solve? Why would one have used it in the first place? What was wrong with loop ? Why is it being removed en masse? What replaces loop , now that it's gone? 回答1: What problems did loop solve? Why would one have used it in the first

What are all these deprecated “loop” parameters in asyncio?

谁都会走 提交于 2020-05-25 07:10:39
问题 A lot of the functions in asyncio have deprecated loop parameters, scheduled to be removed in Python 3.10. Examples include as_completed(), sleep(), and wait(). I'm looking for some historical context on these parameters and their removal. What problems did loop solve? Why would one have used it in the first place? What was wrong with loop ? Why is it being removed en masse? What replaces loop , now that it's gone? 回答1: What problems did loop solve? Why would one have used it in the first