python-asyncio

Waiting for a task to complete after KeyboardInterrupt in asyncio

只愿长相守 提交于 2020-02-01 09:18:28
问题 I'm trying to understand how does asyncio work. In my scenario client makes a tcp connection to the server, sends a login string, if authenticated - receives a stream of chars. Finally on KeyboardInterrupt sends logoff string to the server and hapilly disconnects. Currently I'm stuck on the final part as my logoff method/task is destroyed before it has a chance to complete. ^CTask was destroyed but it is pending! source_traceback: Object created at (most recent call last): File "tst.py", line

Python asyncio subprocess write stdin and read stdout/stderr continuously

五迷三道 提交于 2020-02-01 05:07:26
问题 I'm currently on a task with subprocess in python3 asyncio. My code is simply write to stdin and read stdout/stderr simultaneously: import asyncio async def read_stdout(stdout): print('read_stdout') while True: buf = await stdout.read(10) if not buf: break print(f'stdout: { buf }') async def read_stderr(stderr): print('read_stderr') while True: buf = await stderr.read() if not buf: break print(f'stderr: { buf }') async def write_stdin(stdin): print('write_stdin') for i in range(100): buf = f

How to code consumer.producer with python asyncio?

纵饮孤独 提交于 2020-01-25 04:32:07
问题 My Python version is 3.6.1. I wrote something to implement a model of consumer-producer with Python asyncio. But it doesn't work as expected. Four events all created but none of any print export. async def consumer(queue, id): while True: val = await queue.get() print('{} get a val: {}'.format(id, val)) await asyncio.sleep(1) async def producer(queue, id): for i in range(5): val = random.randint(1, 10) await queue.put(val) print('{} put a val: {}'.format(id, val)) await asyncio.sleep(1) async

Cannot get Jupyter notebook to access javascript variables

丶灬走出姿态 提交于 2020-01-24 22:10:06
问题 I have a website that performs file format conversions, and this service can be used by other websites via the window.postMessage system. It would be great if one could do this also via a Jupyter notebook. I run however into a serious issue. I can get python to create a Javascript command that sends a file in format A to the website, and I can access the response (file in format B) in Javascript after some delay, but I cannot get the response from Javascript back into Python for further

Using async/await keywords with Tk.after() method of tkinter

半城伤御伤魂 提交于 2020-01-24 21:44:08
问题 I am creating a cryptocurrency exchange API client using Python3.5 and Tkinter. I have several displays that I want to update asynchronously every 10 seconds. I am able to update the displays every 10 seconds using Tk.after() like in this example def updateLoans(): offers = dd.loanOffers() demands = dd.loanDemands() w.LoanOfferView.delete(1.0, END) w.LoanDemandView.delete(1.0, END) w.LoanOfferView.insert(END, offers) w.LoanDemandView.insert(END, demands) print('loans refreshed') root.after

sendMessage from outside in autobahn running in separate thread by using “asyncio”

泪湿孤枕 提交于 2020-01-24 20:00:11
问题 I want to call sendMessage method from outside of MyServerProtocol class and send a message to the server. The answare is very similar to this but i need to use asyncio instead of twisted . Cane someone suggest me a solution? An example derived from this would also be appreciated Thanks. 回答1: The call_soon_threadsafe function of event loop is meant for this. from autobahn.asyncio.websocket import WebSocketServerProtocol, \ WebSocketServerFactory class MyServerProtocol(WebSocketServerProtocol)

Await an async function in Python debugger

落爺英雄遲暮 提交于 2020-01-23 05:18:25
问题 Is it possible to await arbitrary calls to an async function when inside a Python debugger? Say I have the following code in some main.py file: import asyncio async def bar(x): return x + 1 async def foo(): import ipdb; ipdb.set_trace() asyncio.run(foo()) Now I want to test calling bar() with some argument inside the debugger to test the results. The following happens: $ python3 main.py > /Users/user/test/main.py(8)foo() 7 import ipdb; ipdb.set_trace() ----> 8 return None 9 ipdb> bar(1)

Python 3.6 ZeroMQ (PyZMQ) asyncio pub sub Hello World

最后都变了- 提交于 2020-01-23 03:09:06
问题 I've just started with ZeroMQ and I'm trying to get a Hello World to work with PyZMQ and asyncio in Python 3.6. I'm trying to de-couple the functionality of a module with the pub/sub code, hence the following class setup: Edit 1 : Minimized example Edit 2 : Included solution, see answer down for how. import asyncio import zmq.asyncio from zmq.asyncio import Context # manages message flow between publishers and subscribers class HelloWorldMessage: def __init__(self, url='127.0.0.1', port='5555

Learning asyncio: “coroutine was never awaited” warning error

落爺英雄遲暮 提交于 2020-01-23 01:12:05
问题 I am trying to learn to use asyncio in Python to optimize scripts. My example returns a coroutine was never awaited warning, can you help to understand and find how to solve it? import time import datetime import random import asyncio import aiohttp import requests def requete_bloquante(num): print(f'Get {num}') uid = requests.get("https://httpbin.org/uuid").json()['uuid'] print(f"Res {num}: {uid}") def faire_toutes_les_requetes(): for x in range(10): requete_bloquante(x) print("Bloquant : ")

How to cancel long-running subprocesses running using `concurrent.futures.ProcessPoolExecutor`?

為{幸葍}努か 提交于 2020-01-22 16:58:05
问题 You can see the full here. A simplified version of my code follows: executor = ProcessPoolExecutor(10) try: coro = bot.loop.run_in_executor(executor, processUserInput, userInput) result = await asyncio.wait_for(coro, timeout=10.0, loop=bot.loop) except asyncio.TimeoutError: result="Operation took longer than 10 seconds. Aborted." Unfortunately, when an operation times out, that process is still running, even though the future has been cancelled. How do I cancel that process/task so that it