Skip to main content
deleted 6 characters in body
Source Link
alex_noname
  • 33.2k
  • 6
  • 95
  • 110

Thread pool works as follows, it starts a task in a thread and until it returns control the thread is busy, even if it sleeps. Therefore, if you want to run them in parallel, you have to either use multiple threads or use asyncio coroutines.

Here is asyncio sample:

import asyncio import time async def foo(): print('Start ...') time.sleep(5) print('... End') async def main(): loop = asyncio.get_running_loop() count = range(2) futures = [loop.run_in_executor(None, foo) for i in count] results = await asyncio.gather(*futures) asyncio.run(main()) 

Output

Start ... Start ... ... End ... End 

Thread pool works as follows, it starts a task in a thread and until it returns control the thread is busy, even if it sleeps. Therefore, if you want to run them in parallel, you have to either use multiple threads or use asyncio coroutines.

Here is asyncio sample:

import asyncio import time async def foo(): print('Start ...') time.sleep(5) print('... End') async def main(): loop = asyncio.get_running_loop() count = range(2) futures = [loop.run_in_executor(None, foo) for i in count] results = await asyncio.gather(*futures) asyncio.run(main()) 

Output

Start ... Start ... ... End ... End 

Thread pool works as follows, it starts a task in a thread and until it returns control the thread is busy, even if it sleeps. Therefore, if you want to run them in parallel, you have to either use multiple threads or use asyncio coroutines.

Here is asyncio sample:

import asyncio import time def foo(): print('Start ...') time.sleep(5) print('... End') async def main(): loop = asyncio.get_running_loop() count = range(2) futures = [loop.run_in_executor(None, foo) for i in count] results = await asyncio.gather(*futures) asyncio.run(main()) 

Output

Start ... Start ... ... End ... End 
added 135 characters in body
Source Link
alex_noname
  • 33.2k
  • 6
  • 95
  • 110

Thread pool works as follows, it starts a task in a thread and until it returns control the thread is busy, even if it sleeps. Therefore, if you want to run them in parallel, you have to either use multiple threads or use asyncio coroutines.

Here is asyncio sample:

import asyncio import time  async def foo(): print('Start ...') await asynciotime.sleep(5) print('... End') async def main(): awaitloop = asyncio.gatherget_running_loop(foo) count = range(2) futures = [loop.run_in_executor(None, foo() for i in count] results = await asyncio.gather(*futures) asyncio.run(main()) 

Output

Start ... Start ... ... End ... End 

Thread pool works as follows, it starts a task in a thread and until it returns control the thread is busy, even if it sleeps. Therefore, if you want to run them in parallel, you have to either use multiple threads or use asyncio coroutines.

Here is asyncio sample:

import asyncio async def foo(): print('Start ...') await asyncio.sleep(5) print('... End') async def main(): await asyncio.gather(foo(), foo()) asyncio.run(main()) 

Output

Start ... Start ... ... End ... End 

Thread pool works as follows, it starts a task in a thread and until it returns control the thread is busy, even if it sleeps. Therefore, if you want to run them in parallel, you have to either use multiple threads or use asyncio coroutines.

Here is asyncio sample:

import asyncio import time  async def foo(): print('Start ...') time.sleep(5) print('... End') async def main(): loop = asyncio.get_running_loop() count = range(2) futures = [loop.run_in_executor(None, foo) for i in count] results = await asyncio.gather(*futures) asyncio.run(main()) 

Output

Start ... Start ... ... End ... End 
added 305 characters in body
Source Link
alex_noname
  • 33.2k
  • 6
  • 95
  • 110

Thread pool works as follows, it starts a task in a thread and until it returns control the thread is busy, even if it sleeps. Therefore, if you want to run them in parallel, you have to either use multiple threads or use asyncio coroutines.

Here is asyncio sample:

import asyncio async def foo(): print('Start ...') await asyncio.sleep(5) print('... End') async def main(): await asyncio.gather(foo(), foo()) asyncio.run(main()) 

Output

Start ... Start ... ... End ... End 

Thread pool works as follows, it starts a task in a thread and until it returns control the thread is busy, even if it sleeps. Therefore, if you want to run them in parallel, you have to either use multiple threads or use asyncio coroutines.

Thread pool works as follows, it starts a task in a thread and until it returns control the thread is busy, even if it sleeps. Therefore, if you want to run them in parallel, you have to either use multiple threads or use asyncio coroutines.

Here is asyncio sample:

import asyncio async def foo(): print('Start ...') await asyncio.sleep(5) print('... End') async def main(): await asyncio.gather(foo(), foo()) asyncio.run(main()) 

Output

Start ... Start ... ... End ... End 
Source Link
alex_noname
  • 33.2k
  • 6
  • 95
  • 110
Loading