1from threading import Thread
2from time import sleep
3# use Thread to run def in background
4# Example:
5def func1():
6 while True:
7 sleep(1)
8 print("Working")
9
10def func2():
11 while True:
12 sleep(2)
13 print("Working2")
14
15if __name__ == '__main__':
16 Thread(target = func1).start()
17 Thread(target = func2).start()
1import multiprocessing
2
3def worker(num):
4 """ Worker procedure
5 """
6 print('Worker:', str(num))
7
8# Mind the "if" instruction!
9if __name__ == '__main__':
10 jobs = [] # list of jobs
11 jobs_num = 5 # number of workers
12 for i in range(jobs_num):
13 # Declare a new process and pass arguments to it
14 p1 = multiprocessing.Process(target=worker, args=(i,))
15 jobs.append(p1)
16 # Declare a new process and pass arguments to it
17 p2 = multiprocessing.Process(target=worker, args=(i+10,))
18 jobs.append(p2)
19 p1.start() # starting workers
20 p2.start() # starting workers