Category : asynchronous

For MySQL v8 and up, it is now possible to perform actual asynchronous operations. The official documentation covers how to do this in C: C API Asynchronous Interface https://dev.mysql.com/doc/c-api/8.0/en/c-api-asynchronous-interface.html There are a collection of "async" MySQL Python libraries, but I’m not sure any are really completely non-blocking. They look to be "pseudo async", and especially ..

Read more

I implemented a paho-mqtt client to do a specific task when a message arrives. Therefore I use the on_message callback function like that: def on_message(): do time-intensive things The callbacks run in one thread in the background because I use a non-blocking loop for mqtt. My question is what exactly happens in this thread? do ..

Read more

I can send files with python’s requests module simply enough with: requests.post(url,files = {‘filename.png’:image_bytes}) However, files isn’t an argument in aiohttp so: async with aiohttp.ClientSession() as session: async with session.post(url, files = my_file) as response: is what I would like to do, but can’t. So what is there a way I can send files through ..

Read more

I copied the following code for my project and it’s worked quite well for me but I don’t really understand how the following code runs my blocking_function: @client.event async def on_message(message): loop = asyncio.get_event_loop() block_response = await loop.run_in_executor(ThreadPoolExecutor(), blocking_function) where on_message is called every time I receive a message. If I receive multiple messages, they ..

Read more

This is my code: async def fun1(): result=False #Some time-consuming operations… return result async def fun2(): result=False #Some time-consuming operations… return result if fun1() or fun2(): print("Success") The if block runs all conditions by order, but I want run them simultaneously and wait until any of theme returns True. I know asyncio.wait(tasks,return_when=asyncio.FIRST_COMPLETED) but I don’t ..

Read more

I am trying to create a framework for a monitor so it can monitor multiple products at once using async. Below I made a simple synchronous version: headers = { ‘user-agent’: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36’, ‘Content-Type’: ‘application/x-www-form-urlencoded’ } def reading(): global urls with open(‘products.txt’, ‘r’) as p: ..

Read more

I am struggling to get this correct. I have a sample piece of code here: import asyncio import signal import os from subprocess import PIPE, Popen async def recScreen(): cmd = ‘ffmpeg -f gdigrab -framerate 30 -i desktop output.mkv’ process = Popen(cmd) await asyncio.sleep(15) print("Done sleep") process.terminate() return 0 loop = asyncio.get_event_loop() loop.run_until_complete(recScreen()) loop.close() The ..

Read more

I’m learning asynchronous programming in Python. I wrote some code to simulate fetching urls in Python and Node; the results are different and I’m not sure why. Python async def asyncFunc(url): #simulate request await asyncio.sleep(3) print(‘woke up…’) async def main(): tasks = [asyncio.create_task( asyncFunc() ) for i in range(3)] for task, i in tasks: await ..

Read more