thread-多线程
multiprocessing-多进程
1 #!/usr/bin/python 2 # -*- coding:utf-8 -*- 3 import os 4 import threading 5 import multiprocessing 6 count_thread = 0 7 count_process = 0 8 9 # worker function 10 def worker1(sign, lock): 11 global count_thread 12 lock.acquire() 13 count_thread += 1 14 print(sign, os.getpid()) 15 lock.release() 16 17 def worker2(sign, lock): 18 global count_process 19 lock.acquire() 20 count_process += 1 21 print(sign, os.getpid()) 22 lock.release() 23 # Main 24 print('Main:',os.getpid()) 25 26 # Multi-thread 27 record = [] 28 lock = threading.Lock() 29 for i in range(5): 30 thread = threading.Thread(target=worker1,args=('thread',lock)) 31 thread.start() 32 record.append(thread) 33 34 for thread in record: 35 thread.join() 36 37 # Multi-process 38 record = [] 39 lock = multiprocessing.Lock() 40 for i in range(5): 41 process = multiprocessing.Process(target=worker2,args=('process',lock)) 42 process.start() 43 record.append(process) 44 45 for process in record: 46 process.join() 47 48 49 print count_thread 50 print count_process