ultiprocessing.Process(target = worker, args = (3,))
11 p.daemon = True
12 p.start()
13 print ("end!")
14
15 #程序运行结果
16 '''
17 end!
18
19 '''
PS:因子进程设置了daemon属性,主进程结束,它们就随着结束了。
3.设置daemon执行完结束的方法
1 import multiprocessing
2 import time
3
4 def worker(interval):
5 print("work start:{0}".format(time.ctime()));
6 time.sleep(interval)
7 print("work end:{0}".format(time.ctime()));
8
9 if __name__ == "__main__":
10 p = multiprocessing.Process(target = worker, args = (3,))
11 p.daemon = True
12 p.start()
13 p.join()
14 print "end!"
15
16 # 结果
17 '''
18 work start:Tue Apr 21 22:16:32 2015
19 work end:Tue Apr 21 22:16:35 2015
20 end!
21 '''
2、Lock
当多个进程需要访问共享资源的时候,Lock可以用来避免访问的冲突。
1 import multiprocessing
2 import sys
3
4 def worker_with(lock, f):
5 with lock:
6 fs = open(f, 'a+')
7 n = 10
8 while n > 1:
9 fs.write("Lockd acquired via with\n")
10 n -= 1
11 fs.close()
12
13 def worker_no_with(lock, f):
14 lock.acquire()
15 try:
16 fs = open(f, 'a+')
17 n = 10
18 while n > 1:
19 fs.write("Lock acquired directly\n")
20 n -= 1
21 fs.close()
22 finally:
23 lock.release()
24
25 if __name__ == "__main__":
26 lock = multiprocessing.Lock()
27 f = "file.txt"
28 w = multiprocessing.Process(target = worker_with, args=(lock, f))
29 nw = multiprocessing.Process(target = worker_no_with, args=(lock, f))
30 w.start()
31 nw.start()
32 print ("end")
3、Semaphore
Semaphore用来控制对共享资源的访问数量,例如池的最大连接数。
1 import multiprocessing
2 import time
3
4 def worker(s, i):
5 s.acquire()
6 print(multiprocessing.current_process().name + "acquire")
7 time.sleep(i)
8 print(multiprocessing.current_process().name + "release\n")
9 s.release()
10
11 if __name__ == "__main__":
12 s = multiprocessing.Semaphore(2) # 限制最多有两个进程同时执行
13 for i in range(5):
14 p = multiprocessing.Process(target = worker, args=(s, i*2))
15 p.start()
运行结果:
1 Process-4acquire
2 Process-2acquire
3 Process-2release
4
5 Process-1acquire
6 Process-1release
7
8 Process-3acquire
9 Process-4release
10
11 Process-5acquire
12 Process-3release
13
14 Process-5release
4、Event
Event实现进程间同步通信
1 import multiprocessing
2 import time
3
4 def wait_for_event(e):
5 print("wait_for_event: starting")
6 e.wait()
7 print("wairt_for_event: e.is_set()->" + str(e.is_set()))
8
9 def wait_for_event_timeout(e, t):
10 print("wait_for_event_timeout:starting")
11 e.wait(t)
12 print("wait_for_event_timeout:e.is_set->" + str(e.is_set()))
13
14 if __name__ == "__main__":
15 e = multiprocessing.Event()
16 w1 = multiprocessing.Process(name = "block",
17 target = wait_for_event,
18 args = (e,))
19
20 w2 = multiprocessing.Process(name = "non-block",
21 target = wait_for_event_timeout,
22 args = (e, 2))
23 w1.start()
24 w2.start()
25
26 time.sleep(3)
27
28 e.set()
29 print("main: event is set")
30
31 # 运行结果
32 '''
33
34 wait_for_event: starting
35
36 wait_for_event_timeout:starting
37
38 wait_for_event_timeout:e.is_set->False
39
40 main: event is set
41
42 wairt_for_event: e.is_set()->True
43
44 '''
5、Queue
Queue是多进程安全的队列,可以使用Queue实现多进程之间的数据传递。put方法用以插入数据到队列中,put方法还有两个可选参数:blocked和timeout。如果bloc