多线程,多进程

# _*_ coding:utf-8 _*_
'''

进程和线程
'''

'''
    单线程
'''

import time
import threading
'''
def music(name,loop):
    for i in range(loop):
        print('Music %s %s'%(name,time.ctime()))
        time.sleep(1)

def movie(name,loop):
    for i in range(loop):
        print('电影 movie %s %s'%(name,time.ctime()))
        time.sleep(1)


if __name__ =='__main__':
    music('爱的故事,',3)
    movie('肖申克的救赎,',4)
    print('endtime %s'%(time.ctime()))
'''
'''
    多线程
'''
'''
def music(name,loop):
    for i in range(loop):
        print('Music %s %s %s'%(name,time.ctime(),threading.Thread.getName(t1)))
        time.sleep(1)

def movie(name,loop):
    for i in range(loop):
        print('电影 movie %s %s %s'%(name,time.ctime(),threading.Thread.getName(t2)))
        time.sleep(1)


#创建多线程
t1 = threading.Thread(target=music,args=('爱的故事,',4))
t1.setName('888888')
t2 = threading.Thread(target=movie,args=('肖申克的救赎,',4),name='9999')
if __name__ =='__main__':
    #守护主线程,主线程结束杀死子线程
    #t1.setDaemon(True)
    #t2.setDaemon(True)
    #开启线程
    t1.start()
    t2.start()

    t1.join()
    #print('\nendtime %s'%(time.ctime()))
    print('\n',t1.ident)
    print(t2.ident)
'''
'''
    加锁
'''
'''
balance = 0
lock = threading.Lock()#获取线程锁
def change(n):
    global balance
    balance += n
    balance -= n
def run_thread(n):
    for i in range(1000000):
        lock.acquire()#获取到锁
        try:
            change(n)
        finally:
            lock.release()#释放锁

t1 = threading.Thread(target=run_thread,args=(4,))
t2 = threading.Thread(target=run_thread,args=(8,))

t1.start()
t2.start()
t1.join()
t2.join()

print(balance)
'''


'''
    单进程 按时间顺序
'''

import multiprocessing
'''
def work_1(f,n):
    print('work_1 start')
    for i in range(n):
        with open(f,'a') as fs:
            fs.write('i love python \n')
            time.sleep(1)
    print('work_1 end')

def work_2(f,n):
    print('work_2 start')
    for i in range(n):
        with open(f,'a') as fs:
            fs.write('come on baby \n')
            time.sleep(1)
    print('work_2 end')

if __name__ == '__main__':
    work_1('file.txt',3)
    work_2('file.txt',3)
'''

'''
    多进程加锁
'''
'''
def work_1(f,n,lock):
    print('work_1 start')
    lock.acquire()
    for i in range(n):
        with open(f,'a') as fs:
            fs.write('i love python \n')
            time.sleep(1)
    print('work_1 end')
    lock.release()
def work_2(f,n,lock):
    print('work_2 start')
    lock.acquire()
    for i in range(n):
        with open(f,'a') as fs:
            fs.write('come on baby \n')
            time.sleep(1)
    print('work_2 end')
    lock.release()

if __name__ == '__main__':
    lock = multiprocessing.Lock()
    p1 = multiprocessing.Process(target=work_1,args=('file.txt',3,lock))
    p2 = multiprocessing.Process(target=work_2, args=('file.txt', 3,lock))
    p1.start()
    p2.start()
'''

'''
    线程池
'''
'''
import os
def work(n):
    print('run work (%s),work id %s'%(n,os.getpid()))
    time.sleep(5)
    print('work (%s) stop,work id %s'%(n,os.getpid()))

if __name__ == '__main__':
    print('Parent process %s.'%os.getpid())
    p = multiprocessing.Pool(3)#创建进程池
    for i in range(5):
        #创建5个进程,一次进入进程池
        p.apply_async(work,args=(i,))
    p.close()
    p.join()
'''

'''
    Queue队列
'''
#queue跨进程通信
'''
def put(q):
    for value in ['A','B','C','D']:
        print('发送 %s 到 queue...'%value)
        q.put(value) #通过put 插入数据发送
        time.sleep(2)
def get(q):
    while(True):
        value = q.get(True)#接受队列中的数据
        print('从 queue 接受 %s.' %value)

if __name__ == '__main__':
    #父进程创建Queue,并传给子进程,此时的q相当于中间的媒介
    q = multiprocessing.Queue()
    pw = multiprocessing.Process(target=put,args=(q,))#进程1
    pr = multiprocessing.Process(target=get,args=(q,))#进程2

    #pw 和 pr 进行通信

    #启动子进程pw,写入
    pw.start()

    #启动子进程pr,读取
    pr.start()

    #等待pw结束
    pw.join()

    # pr进程里是死循环,无法等待其结束,只能进行强行终止
    pr.terminate()
'''

'''
发送 A 到 queue...
从 queue 接受 A.
发送 B 到 queue...
从 queue 接受 B.
发送 C 到 queue...
从 queue 接受 C.
发送 D 到 queue...
从 queue 接受 D.
'''

'''
    Pipe 管道通信
'''
'''
def put(p):
    for value in ['A','B','C','D']:
        print('发送 %s 到 pipe...'%value)
        p[1].send(value) #send 发送数据
        time.sleep(2)
def get(p):
    while(True):
        value = p[0].recv()#接受管道中的数据
        print('从 pipe 接受 %s.' %value)

if __name__ == '__main__':
    #父进程创建Queue,并传给子进程,此时的p含有两个方向的点
    p = multiprocessing.Pipe(duplex=False)#第一参数收,第二参数发:左收右发
    pw = multiprocessing.Process(target=put,args=(p,))#进程1
    pr = multiprocessing.Process(target=get,args=(p,))#进程2

    #pw 和 pr 进行通信

    #启动子进程pw,写入
    pw.start()

    #启动子进程pr,读取
    pr.start()

    #等待pw结束
    pw.join()

    # pr进程里是死循环,无法等待其结束,只能进行强行终止
    pr.terminate()
'''
'''
发送 A 到 pipe...
从 pipe 接受 A.
发送 B 到 pipe...
从 pipe 接受 B.
发送 C 到 pipe...
从 pipe 接受 C.
发送 D 到 pipe...
从 pipe 接受 D.
'''

'''
queue.Queue是进程内非阻塞队列(线程间通信)
multiprocessing.Queue()跨进程间通信
'''

''''
    生产者消费者模型
'''
import queue
q = queue.Queue(maxsize=10)

def producer(name):#生产者
    count = 1
    while True:
        q.put('骨头%s'%count)
        print('生产了骨头%d'%count)
        count +=1
        time.sleep(1)

def consumer(name):#消费者
    while True:
        print('[%s] 取到[%s]并且吃了它...'%(name,q.get()))
        time.sleep(1)

p = threading.Thread(target=producer,args=('Tim_producer',))
c1 = threading.Thread(target=consumer,args=('zhang_consumer',))
c2 = threading.Thread(target=consumer,args=('zhu_consumer',))

p.start()
c1.start()
c2.start()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三木森森の

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值