Thread
Like other scripting languages, python threads are good for IO-bound tasks, but not ideal for CPU-bound tasks.
import threading
import time
def task(id):
time.sleep(1)
print(id)
threads = [threading.Thread(target=task, args=(i,)) for i in range(5)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
Output:
3
2
1
4
0