python多线程多进程的常用操作。

  1. 调用进程
  2. 进程池 (由于启动线程的开销比较小,所以不需要线程池这种概念,多线程只会频繁得切换cpu导致系统变慢,并不会占用过多的内存空间)
  3. 进程通信 Pipe,Queue
  4. 调用线程
  5. lock
  6. 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()#One must call close() or terminate() before using join().
    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()
    # 使用到的全局变量只是作为引用,不在函数中修改它的值的话,不需要加global关键字。
    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')
    # call_thread_with_recursive()
    # call_thread_with_lock()
    # call_threads()
    # queue_()
    # call_proc()
    # call_pool()
    # pipe_()
    print('end')





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值