- 调用进程
- 进程池 (由于启动线程的开销比较小,所以不需要线程池这种概念,多线程只会频繁得切换cpu导致系统变慢,并不会占用过多的内存空间)
- 进程通信 Pipe,Queue
- 调用线程
- lock
- threading.local()
在一个线程内,递归调用函数时,传递参数显得复杂,因此把参数保存在一个字典里,只有本线程可以访问该字典,别的线程不行。
"""
1. 调用进程
------------------------------------------------------
"""
from multiprocessing import Process
import os
from time import sleep
def proc(info):
sleep(2)
print('proc %s %s'%(os.getpid(),info))
def call_proc():
p=Process(target=proc,args=('有内鬼。。。',))
p.start()
p.join()
"""
---------------------------------------------------------
2. 进程池
(由于启动线程的开销比较小,所以不需要线程池这种概念,多线程只会频繁得切换cpu导致系统变慢,并不会占用过多的内存空间)
---------------------------------------------------------
"""
from multiprocessing import Pool
def call_pool():
p=Pool(5)
for i in range(5):
p.apply_async(proc,args=('有内鬼,终止交易!',))
p.close()
p.join()
"""
-------------------------------------------------------------
4. 进程通信 Pipe,Queue
-------------------------------------------------------------
"""
from multiprocessing import Pipe
def send_msg(in_):
in_.send('天王盖地虎')
def recv_msg(out_):
print('%s; 宝塔镇河妖!'%out_.recv())
def pipe_():
in_,out_=Pipe()
p1=Process(target=send_msg,args=(in_,))
p2=Process(target=recv_msg,args=(out_,))
p1.start()
p2.start()
p1.join()
p2.join()
from multiprocessing import Queue
def read_queue(q):
while True:
print('%s'%q.get(True))
def write_queue(q):
q.put('机器学习')
sleep(1)
def queue_():
q=Queue()
p1=Process(target=read_queue,args=(q,))
p2=Process(target=write_queue,args=(q,))
p1.start()
p2.start()
p2.join()
p1.terminate()
"""
---------------------------------------------------
4. 调用线程
--------------------------------------------------
"""
from threading import Thread
import threading
def th1(x):
print('%s starts, x is %s'%(threading.current_thread().name,x))
sleep(1)
def th2(x):
print('%s starts, x is %s'%(threading.current_thread().name,x))
sleep(1)
def call_threads():
t1=Thread(target=th1,args=('t1',),name='thread1')
t2=Thread(target=th2,args=('t2',),name='thread2')
t1.start()
t2.start()
t1.join()
t2.join()
"""
---------------------------------------------------------------------
5. lock
---------------------------------------------------------------------
"""
from threading import Lock
count=1
l=Lock()
def chang_it():
global count
count-=1
count+=1
def run_with_lock():
for i in range(1000000):
with l:
chang_it()
print('count=%d'%count)
def call_thread_with_lock():
t1=Thread(target=run_with_lock,name='thread1')
t2=Thread(target=run_with_lock,name='thread2')
t1.start()
t2.start()
t1.join()
t2.join()
"""
----------------------------------------------------------------------------------
6. threading.local()
在一个线程内,递归调用函数时,传递参数显得复杂,因此把参数保存在一个字典里,只有本线程可以
访问该字典,别的线程不行。
----------------------------------------------------------------------------------
"""
loc=threading.local()
def func1(str):
loc.func1=str+'_func1'
loc.func2=str+'_func2'
loc.func3=str+'_func3'
print(loc.func1)
func2()
def func2():
print(loc.func2)
func3()
def func3():
print(loc.func3)
def call_thread_with_recursive():
t1=Thread(target=func1,args=('t1',))
t2=Thread(target=func1,args=('t2',))
t1.start()
t2.start()
t1.join()
t2.join()
"""
--------------------------------------------------
7. 信号量(BoundedSemaphore类) to be continue...
-------------------------------------------------
"""
if __name__=='__main__':
print('main')
print('end')