python-asyncio

Testing aiohttp client with unittest.mock.patch

倾然丶 夕夏残阳落幕 提交于 2021-02-20 09:19:53
问题 I've written a simple HTTP client using aiohttp and I'm trying to test it by patching aiohttp.ClientSession and aiohttp.ClientResponse . However, it appears as though the unittest.mock.patch decorator is not respecting my asynchronous code. At a guess, I would say it's some kind of namespacing mismatch. Here's a minimal example: from aiohttp import ClientSession async def is_ok(url:str) -> bool: async with ClientSession() as session: async with session.request("GET", url) as response: return

Testing aiohttp client with unittest.mock.patch

天大地大妈咪最大 提交于 2021-02-20 09:18:29
问题 I've written a simple HTTP client using aiohttp and I'm trying to test it by patching aiohttp.ClientSession and aiohttp.ClientResponse . However, it appears as though the unittest.mock.patch decorator is not respecting my asynchronous code. At a guess, I would say it's some kind of namespacing mismatch. Here's a minimal example: from aiohttp import ClientSession async def is_ok(url:str) -> bool: async with ClientSession() as session: async with session.request("GET", url) as response: return

asyncio not working on Google Cloud Functions

半腔热情 提交于 2021-02-20 02:57:43
问题 I have this function which works fine locally on my machine with python 3.8, but it throws runtime error on Google Cloud Functions. def telegram_test(request): request_json = request.get_json() import datetime import pandas as pd from pyrogram import Client session_string = "...............38Q8uTHG5gHwyWD8nW6h................." # the rest of the authantication api_id = 32494131641215 api_hash = "ioadsfsjnjksfgnfriuthg#qw]/zwq ]w/\lc ec," # one of bbc channels on telegram you want to access

How do I set the asyncio event loop for a thread in Python?

為{幸葍}努か 提交于 2021-02-18 20:41:06
问题 I'm trying to create two threads that each have their own asyncio event loop. I've tried the following code but it doesn't seem to work: import asyncio from threading import Thread def hello(thread_name): print('hello from thread {}!'.format(thread_name)) event_loop_a = asyncio.new_event_loop() event_loop_b = asyncio.new_event_loop() def callback_a(): asyncio.set_event_loop(event_loop_a) asyncio.get_event_loop().call_soon_threadsafe(lambda: hello('a')) def callback_b(): asyncio.set_event_loop

Python 3.6: async version of islice?

若如初见. 提交于 2021-02-18 07:41:24
问题 I'm trying to do something like this: import asyncio from itertools import islice async def generate_numbers(n): for x in range(n): yield x async def consume_numbers(n): async for x in generate_numbers(n): print(x) async def consume_some_numbers(n,m): async for x in islice(generate_numbers(n),m): #<-- This doesn't work. islice doesn't recognize async iterators as iterators. print(x) loop = asyncio.get_event_loop() loop.run_until_complete(consume_numbers(10)) loop.run_until_complete(consume

Running multiple sockets using asyncio in python

烂漫一生 提交于 2021-02-17 01:53:43
问题 Setup: Python 3.7.4 I am trying to create 6 sockets using asyncio listening on different ports. I tried to implement it like this. Code: import asyncio async def client_thread(reader, writer): while True: package_type = await reader.read(100) if not package_type: break if(package_type[0] == 1) : nn_output = get_some_bytes1 elif (package_type[0] == 2) : nn_output = get_some_bytes2 elif (package_type[0] == 3) : nn_output = get_some_bytes3 elif (package_type[0] == 4) : nn_output = get_some

Running multiple sockets using asyncio in python

本秂侑毒 提交于 2021-02-17 01:52:24
问题 Setup: Python 3.7.4 I am trying to create 6 sockets using asyncio listening on different ports. I tried to implement it like this. Code: import asyncio async def client_thread(reader, writer): while True: package_type = await reader.read(100) if not package_type: break if(package_type[0] == 1) : nn_output = get_some_bytes1 elif (package_type[0] == 2) : nn_output = get_some_bytes2 elif (package_type[0] == 3) : nn_output = get_some_bytes3 elif (package_type[0] == 4) : nn_output = get_some

Future from asyncio.run_coroutine_threadsafe hangs forever?

核能气质少年 提交于 2021-02-16 21:07:51
问题 As a followup to my previous question about calling an async function from a synchronous one, I've discovered asyncio.run_coroutine_threadsafe . On paper, this looks ideal. Based on the comments in this StackOverflow question, this looks ideal. I can create a new Thread, get a reference to my original event loop, and schedule the async function to run inside the original event loop while only blocking the new Thread. class _AsyncBridge: def call_async_method(self, function, *args, **kwargs):

Future from asyncio.run_coroutine_threadsafe hangs forever?

痞子三分冷 提交于 2021-02-16 21:07:19
问题 As a followup to my previous question about calling an async function from a synchronous one, I've discovered asyncio.run_coroutine_threadsafe . On paper, this looks ideal. Based on the comments in this StackOverflow question, this looks ideal. I can create a new Thread, get a reference to my original event loop, and schedule the async function to run inside the original event loop while only blocking the new Thread. class _AsyncBridge: def call_async_method(self, function, *args, **kwargs):

Future from asyncio.run_coroutine_threadsafe hangs forever?

久未见 提交于 2021-02-16 21:06:57
问题 As a followup to my previous question about calling an async function from a synchronous one, I've discovered asyncio.run_coroutine_threadsafe . On paper, this looks ideal. Based on the comments in this StackOverflow question, this looks ideal. I can create a new Thread, get a reference to my original event loop, and schedule the async function to run inside the original event loop while only blocking the new Thread. class _AsyncBridge: def call_async_method(self, function, *args, **kwargs):