I'm trying to execute the code on Python in parallel using asyncio. The idea is to run multiple jobs in parallel.
Here is my code:
import asyncio
import threading
async def print_thread():
for n in range(5):
print("Number: {}".format(threading.get_ident()))
if __name__ == '__main__':
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(print_thread())
finally:
loop.close()
The output is:
Number: 4599266752
Number: 4599266752
Number: 4599266752
Number: 4599266752
Number: 4599266752
As far as I understand the code has been executed on a single thread. Is there a way to parallelize it?
PS
If I change the code to:
async def print_thread():
print("Number: {}".format(threading.get_ident()))
if __name__ == '__main__':
loop = asyncio.get_event_loop()
try:
for n in range(5):
loop.run_until_complete(print_thread())
I get the same result.