進程:
單個進程:
from multiprocessing import Process
import time
def task(msg):
print ('hello, %s' % msg)
time.sleep(1)
if __name__ == '__main__':
p = Process(target=task, args=('world',))
p.start()
if p.is_alive():
print ('Process: %s is running' % p.pid)
p.join()
多進程
from multiprocessing import Process
import time
def task(msg):
print ('hello, %s' % msg)
time.sleep(1)
if __name__ == '__main__':
for i in range(5):
p = Process(target=task, args=('world',))
p.start()
if p.is_alive():
print ('Process: %s is running' % p.pid)
p.join()
使用進程池,進程在運行時最多不能超過進程池的大小
from multiprocessing import Pool
import os,time
def run_task(name):
print('task',os.getpid())
time.sleep(2)
print('task',os.getpid(),' is end')
if __name__=='__main__':
p=Pool(processes=3)
for i in range(5):
p.apply_async(run_task,args=(str(i),))
p.close()
p.join()
print('all is done')
進程之間的通信:
Queue:多進程之間通信:
from multiprocessing import Process,Queue
import os,time,random
def proc_write(q,urls):
print('Process (%s) is writing' % os.getpid())
for url in urls:
q.put(url)
print('put %s to queue' % url)
time.sleep(random.random())
def proc_read(q):
print('Process (%s) is reading' % os.getpid())
while True:
url=q.get(True)
print('Get %s from queue' % url)
if __name__=='__main__':
q=Queue()
proc_write1=Process(target=proc_write,args=(q,['url_1','url_2','url_3']))
proc_write2=Process(target=proc_write,args=(q,['url_4','url_5','url_6']))
proc_reader=Process(target=proc_read,args=(q,))
proc_write1.start()
proc_write2.start()
proc_reader.start()
proc_write1.join()
proc_write2.join()
proc_reader.terminate()
Pipe:管道,兩個進程之間的通信,一個send,一個revc
import os,time,random,multiprocessing
def proc_send(pipe,urls):
for url in urls:
print('Process (%s) is sending %s.' % (os.getpid(),url))
pipe.send(url)
time.sleep(random.random())
def proc_read(pipe):
while True:
try:
msg=pipe.recv()
print('Process (%s) rev %s.' % (os.getpid(),msg))
except EOFError:
break
if __name__=='__main__':
pipe=multiprocessing.Pipe()
p1=multiprocessing.Process(target=proc_send,args=(pipe[0],['url_'+str(i) for i in range(10)]))
p2=multiprocessing.Process(target=proc_read,args=(pipe[1],))
p1.start()
p2.start()
pipe[0].close()
pipe[1].close()
p1.join()
p2.join()
線程:
多線程
import time,threading
def music(name):
time.sleep(3)
print ('播放歌曲 %s at %s.' % (name,str(time.time())))
def movie(name):
time.sleep(3)
print('播放電影 %s at %s.' % (name,str(time.time())))
threads=[]
t1=threading.Thread(target=music,args=(u'沙漠駱駝',))
threads.append(t1)
t2=threading.Thread(target=movie,args=(u'影',))
threads.append(t2)
if __name__=='__main__':
for t in threads:
t.setDaemon(True)
t.start()
t.join()
print('time is',time.time())
lock同步
import time,threading
lock=threading.Lock()
v=1000
def b(n):
lock.acquire()
try:
global v
v+=n
print(threading.current_thread(),'is running.v:',v)
finally:
lock.release()
t1=threading.Thread(target=b,args=(6,))
t2=threading.Thread(target=b,args=(8,))
t3=threading.Thread(target=b,args=(8,))
t1.start()
t2.start()
t3.start()
t1.join()
t2.join()
t3.join()
print(v)