python-asyncio

Return an asyncio callback result from task creating function

老子叫甜甜 提交于 2020-07-23 08:20:08
问题 I'm trying to wrap an async function up so that I can use it without importing asyncio in certain files. The ultimate goal is to use asynchronous functions but being able to call them normally and get back the result. How can I access the result from the callback function printing(task) and use it as the return of my make_task(x) function? MWE: #!/usr/bin/env python3.7 import asyncio loop = asyncio.get_event_loop() def make_task(x): # Can be used without asyncio task = loop.create_task(my

How can I use gRPC with asyncio

左心房为你撑大大i 提交于 2020-07-22 04:37:20
问题 Where can I find examples of using gRPC with asyncio In particular, how to create a client using gRPC and asyncio 回答1: gRPC Python is currently not compatible with asyncio. See dicussion/feature request at https://github.com/grpc/grpc/issues/6046. 回答2: The feature request mentioned by @Eric G has resulted in an asyncio API being added to the gRPC Python API. This API, however, is still experimental, so the only examples currently available are tests buried in the gRPC repo. Since you're

How to stop loop running in executor?

ⅰ亾dé卋堺 提交于 2020-07-19 11:21:05
问题 I am running function that takes time to finish. The user has a choice to stop this function/event. Is there an easy way to stop the thread or loop? class ThreadsGenerator: MAX_WORKERS = 5 def __init__(self): self._executor = ThreadPoolExecutor(max_workers=self.MAX_WORKERS) self.loop = None self.future = None def execute_function(self, function_to_execute, *args): self.loop = asyncio.get_event_loop() self.future = self.loop.run_in_executor(self._executor, function_to_execute, *args) return

Asyncio task vs coroutine

不羁岁月 提交于 2020-07-18 21:09:26
问题 Reading the asyncio documentation, I realize that I don't understand a very basic and fundamental aspect: the difference between awaiting a coroutine directly, and awaiting the same coroutine when it's wrapped inside a task. In the documentation examples the two calls to the say_after coroutine are running sequentially when awaited without create_task , and concurrently when wrapped in create_task . So I understand that this is basically the difference, and that it is quite an important one.

discord.py problems with if not arg (clear command)

大城市里の小女人 提交于 2020-07-16 08:05:00
问题 I have a problem with if not arg (I skip it directly) and I understand that putting arg: int creates problems for me. Do you know a solution? I tried with many methods but I can't Code: @client.command(pass_context=True) @commands.has_permissions(administrator=True) async def clear(ctx, arg: int): if not arg: embed = discord.Embed( color=discord.Colour.red() ) embed.set_author( name="Specifica quante messaggi vuoi cancellare!", icon_url="https://cdn.discordapp.com/attachments

Synchronous generator in asyncio

蓝咒 提交于 2020-07-09 14:34:39
问题 I have the following scenario: I have a blocking, synchronous generator I have an non-blocking, async function I would like to run blocking generator (executed in a ThreadPool ) and the async function on the event loop. How do I achieve this? The following function simply prints the output from the generator, not from sleep function. Thanks! from concurrent.futures import ThreadPoolExecutor import numpy as np import asyncio import time def f(): while True: r = np.random.randint(0, 3) time

AWS lambda with python asyncio. Event loop closed problem?

这一生的挚爱 提交于 2020-07-09 12:52:45
问题 Closing the event loop in aws lambda affects future lambda runs?? I have some aysncio python code running within an aws lambda service. The logic of the code is as follows def lambda_handler(event,context): loop = asyncio.get_event_loop() # perform all operations with the loop loop.close() return results If I run this once, it appears to work fine. However, if I rerun it immediately afterwards, I get an error saying Event loop closed Why is this happening? Shouldn't each lambda run be

Asyncio in corroutine RuntimeError: no running event loop

不羁岁月 提交于 2020-07-07 04:08:11
问题 I'm writing multi-process code, which runs perfectly in Python 3.7. Yet I want one of the parallel process to execute an IO process take stakes for ever using AsyncIO i order to get better performance, but have not been able to get it to run. Ubuntu 18.04, Python 3.7, AsyncIO, pipenv (all pip libraries installed) The method in particular runs as expected using multithreading, which is what I want to replace with AsyncIO. I have googled and tried looping in the main() function and now only in

How to use an async for loop to iterate over a list?

我是研究僧i 提交于 2020-07-05 01:25:10
问题 So I need to call an async function for all items in a list. This could be a list of URLs and an async function using aiohttp that gets a response back from every URL. Now obviously I cannot do the following: async for url in ['www.google.com', 'www.youtube.com', 'www.aol.com']: I can use a normal for loop but then my code will act synchronously and I lose the benefits and speed of having an async response fetching function. Is there any way I can convert a list such that the above works? I

Get aiohttp results as string

不问归期 提交于 2020-06-29 08:28:33
问题 I'm trying to get data from a website using async in python. As an example I used this code (under A Better Coroutine Example): https://www.blog.pythonlibrary.org/2016/07/26/python-3-an-intro-to-asyncio/ Now this works fine, but it writes the binary chunks to a file and I don't want it in a file. I want the resulting data directly. But I currently have a list of coroutine objects which I can not get the data out of. The code: # -*- coding: utf-8 -*- import aiohttp import asyncio import async