How to run multiple threads python code example
Example 1: python running multiple threads at the same time
from threading import Thread
from time import sleep
def func1():
while True:
sleep(1)
print("Working")
def func2():
while True:
sleep(2)
print("Working2")
if __name__ == '__main__':
Thread(target = func1).start()
Thread(target = func2).start()
Example 2: how to run same function on multiple threads in pyhton
import multiprocessing
def worker(num):
""" Worker procedure
"""
print('Worker:', str(num))
if __name__ == '__main__':
jobs = []
jobs_num = 5
for i in range(jobs_num):
p1 = multiprocessing.Process(target=worker, args=(i,))
jobs.append(p1)
p2 = multiprocessing.Process(target=worker, args=(i+10,))
jobs.append(p2)
p1.start()
p2.start()