python-asyncio

Start an async function inside a new thread

喜欢而已 提交于 2021-02-05 12:16:35
问题 I'm trying to create a discord bot and I need to run an async function in another new Thread since the main Thread is required to run another function (Discord Client) What I'm trying to accomplish: # This methods needs to run in another thread async def discord_async_method(): while True: sleep(10) print("Hello World") ... # Discord Async Logic # This needs to run in the main thread client.run(TOKEN) thread = "" try: # This does not work, throws error "printHelloWorld Needs to be awaited"

Close a task in Python based on condition

我与影子孤独终老i 提交于 2021-02-05 11:24:37
问题 I'm using AsyncIO and the Websockets module to create two concurrent tasks in Python, each one connects to a websocket server and receives messages. I'm trying to create a system where, when a task did not receive messages for more than 4 seconds, it must close the connection and the task, but i'm having an hard time figuring that out. Can anyone help me out on this? Here is what i tried: async def connect(URI): async with websockets.client.connect(URI) as ws: LastUpdate = time.time() while

Close a task in Python based on condition

≡放荡痞女 提交于 2021-02-05 11:24:07
问题 I'm using AsyncIO and the Websockets module to create two concurrent tasks in Python, each one connects to a websocket server and receives messages. I'm trying to create a system where, when a task did not receive messages for more than 4 seconds, it must close the connection and the task, but i'm having an hard time figuring that out. Can anyone help me out on this? Here is what i tried: async def connect(URI): async with websockets.client.connect(URI) as ws: LastUpdate = time.time() while

How to use both Python multiprocessing and asyncio?

心不动则不痛 提交于 2021-02-05 09:39:21
问题 import asyncio import httpx from datetime import datetime async def request_test(url): async with httpx.AsyncClient() as client: r = await client.get(url, timeout=None, headers=None) return len(r.text) async def main(rest_api_url_list ): futures = [asyncio.ensure_future(request_test(url)) for url in rest_api_url_list ] results = await asyncio.gather(*futures) print(results) print(len(results)) start = datetime.now() rest_api_url_list = [~~~~~~~~~~~~~] # 2000EA loop = asyncio.get_event_loop()

embed a aiohttp server in a PyQt application

戏子无情 提交于 2021-02-05 08:08:47
问题 I am going to embed a aiohttp server in a PyQt application, but when I run the code below , the Qt window couldn't show, I know it was caused by web.run_app(app) , I've tried to move it into a thread , but then I got RuntimeError: There is no current event loop in thread 'Dummy-1' , so what should I do ? I've found asyncqt which might help ,but I don't know how to use it to deal with a aiohttp server. from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * from

Python async: Waiting for stdin input while doing other stuff

一个人想着一个人 提交于 2021-02-05 06:40:47
问题 I'm trying to create a WebSocket command line client that waits for messages from a WebSocket server but waits for user input at the same time. Regularly polling multiple online sources every second works fine on the server, (the one running at localhost:6789 in this example), but instead of using Python's normal sleep() method, it uses asyncio.sleep() , which makes sense because sleeping and asynchronously sleeping aren't the same thing, at least not under the hood. Similarly, waiting for

RuntimeWarning: Enable tracemalloc to get the object allocation traceback with asyncio.sleep

主宰稳场 提交于 2021-02-04 12:08:17
问题 Trying to use a semaphore to control asynchronous requests to control the requests to my target host but I am getting the following error which I have assume means that my asycio.sleep() is not actually sleeping. How can I fix this? I want to add a delay to my requests for each URL targeted. Error: RuntimeWarning: coroutine 'sleep' was never awaited Coroutine created at (most recent call last) File "sephora_scraper.py", line 71, in <module> loop.run_until_complete(main()) File "/Library

RuntimeWarning: Enable tracemalloc to get the object allocation traceback with asyncio.sleep

三世轮回 提交于 2021-02-04 12:07:57
问题 Trying to use a semaphore to control asynchronous requests to control the requests to my target host but I am getting the following error which I have assume means that my asycio.sleep() is not actually sleeping. How can I fix this? I want to add a delay to my requests for each URL targeted. Error: RuntimeWarning: coroutine 'sleep' was never awaited Coroutine created at (most recent call last) File "sephora_scraper.py", line 71, in <module> loop.run_until_complete(main()) File "/Library

Python Websockets-8.1 ConnectionClosedError

ぐ巨炮叔叔 提交于 2021-01-29 08:52:00
问题 I want to learn how to properly register and unregister multiple client when using websockets-8.1 as both producer and consumer. The code below, using websockets-8.1 acting as both producer and consumer, throws an exception when a client closes the connection (refreshing or closing the browser window): future: <Task finished coro=<WebSocketCommonProtocol.send() done, defined at .../site-packages/websockets/protocol.py:521> exception=ConnectionClosedError('code = 1006 (connection closed

Why is my asynchronous version of my python script not faster than the synchronous version?

风流意气都作罢 提交于 2021-01-29 07:06:32
问题 I'm just starting experimenting with the asyncio library in python. My purpose is to speed up my code, however with my first script there really is no improvement than doing it without asyncio. from yahoo_fin import stock_info as si from yahoo_fin.stock_info import * import time import asyncio async def price(stock): prijs = str(si.get_live_price(stock)) await asyncio.sleep(0.001) print(prijs) def main(): loop = asyncio.get_event_loop() t0 = time.time() task = asyncio.gather( price('aapl'),