-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulProcess.py
51 lines (42 loc) · 1.14 KB
/
mulProcess.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import random
from threading import Thread
from multiprocessing import Process
myList=[]
size=10000000
threads=2
for i in range(threads):
myList.append([])
def func(count,mylist):
for i in range(count):
mylist.append(random.random())
def simple():
for i in range(threads):
func(size,myList[i])
#normal without using threading
#multiThreading
def multithreaded():
jobs=[]
for i in range(threads):
thread=Thread(target=func,args=(size,myList[i]))
jobs.append(thread)
#start the threads
for j in jobs:
j.start()
#ensure all thread have finished execution
for j in jobs:
j.join()
def multiprocessed():
processes=[]
for i in range(threads):
p=Process(target=func,args=(size,myList[i]))
processes.append(p)
#start the processes
for p in processes:
p.start()
#ensure all processes have finished execution
for p in processes:
p.join()
if __name__=="__main__":
multiprocessed() #faster
# simple() #slower than multipreocess
#thread() #slower than simple process