进程
1 - 进程数据是不共享的
data_list = [ ]
def task ( arg) :
data_list. append ( arg)
print ( data_list)
def run ( ) :
for i in range ( 10 ) :
p = multiprocessing. Process ( target= task, args= ( i, ) )
# p = threading. Thread ( target= task, args= ( i, ) )
p. start ( )
if __name__ == '__main__' :
run ( )
2- 如何数据共享
1:queue共享
linux:
import multiprocessing
q = multiprocessing. Queue ( )
def task ( arg, q) :
q. put ( arg)
def run ( ) :
for i in range ( 10 ) :
p = multiprocessing. Process ( target= task, args= ( i, q, ) )
p. start ( )
while True:
v = q. get ( )
print ( v)
run ( )
windows:
def task ( arg, q) :
q. put ( arg)
if __name__ == '__main__' :
q = multiprocessing. Queue ( )
for i in range ( 10 ) :
p = multiprocessing. Process ( target= task, args= ( i, q, ) )
p. start ( )
while True:
v = q. get ( )
print ( v)
2-Manager共享
Linux:
import multiprocessing
m = multiprocessing. Manager ( )
dic = m. dict ( )
def task ( arg) :
dic[ arg] = 100
def run ( ) :
for i in range ( 10 ) :
p = multiprocessing. Process ( target= task, args= ( i, ) )
p. start ( )
input ( '>>>' )
print ( dic. values ( ) )
if __name__ == '__main__' :
run ( )
windows:
def task ( arg, dic) :
time. sleep ( 2 )
dic[ arg] = 100
if __name__ == '__main__' :
m = multiprocessing. Manager ( )
dic = m. dict ( )
process_list = [ ]
for i in range ( 10 ) :
p = multiprocessing. Process ( target= task, args= ( i, dic, ) )
p. start ( )
process_list. append ( p)
while True:
count = 0
for p in process_list:
if not p. is_alive ( ) :
count += 1
if count == len ( process_list) :
break
print ( dic)
3-进程锁
import time
import threading
import multiprocessing
lock = multiprocessing. RLock ( )
#这里用的是multiproceesing
def task ( arg) :
print ( '鬼子来了' )
lock. acquire ( )
time. sleep ( 2 )
print ( arg)
lock. release ( )
if __name__ == '__main__' :
p1 = multiprocessing. Process ( target= task, args= ( 1 , ) )
p1. start ( )
p2 = multiprocessing. Process ( target= task, args= ( 2 , ) )
p2. start ( )
4-进程池
import time
from concurrent. futures import ThreadPoolExecutor, ProcessPoolExecutor
def task ( arg) :
time. sleep ( 2 )
print ( arg)
if __name__ == '__main__' :
pool = ProcessPoolExecutor ( 5 )
for i in range ( 10 ) :
pool. submit ( task, i)