I am trying to run a multi-threaded program with asyncio, but I am failing at the part of adding threads. The program runs ok with asyncio as it is:
async def main(var1, var2):
tasks = list()
for z in var1:
for x in range(5):
tasks.append(get_ip(z, var2))
return await asyncio.gather(*tasks)
loop = asyncio.get_event_loop()
start_time = time.time()
for x in list:
result = loop.run_until_complete(main(x, list2))
loop.run_until_complete(release_main(result))
loop.close()
I want to have the for x in list: in threads, I have 8 CPUs so I would want to run it with 8 threads like for example using: with concurrent.futures.ThreadPoolExecutor(max_workers=8) as executor:.
I have been reading posts and everything but I either mess the result from result, or break something and doesn't work. Help/tips needed.
await loop.run_in_executor() doesn't work if I don't have that in a function, is it really needed? when I add the above code in a function and call it it breaks everything
call_soon_threadsafe. So you would need to manually spawn threads, manually spawn a separate loop in each thread, run whatever you need and finally synchronize results afterwards. But really, due to GIL this doesn't seem to be worth it anyway. Python is quite bad at parallel execution.asyncio.run_coroutine_threadsafe()but it just doesn't run my program with that.