首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 多进程 多线程数据共享

Python 多进程 多线程数据共享

作者头像
py3study
发布2020-01-09 16:37:32
7850
发布2020-01-09 16:37:32
举报
文章被收录于专栏:python3python3
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Changhua Gong
from multiprocessing import Process, Queue
import os, time, random
'''
1. 我们平时from queue import Queue是线程对列,用于数据共享的,只能在线程之间进行使用;
2. from multiprocessing import Queue,是进程对列,用于进程间数据交换,实际中是在进程之间进行序列化和反序列化(pickle)
   完成数据交互的;
3. 线程之间修改同一份数据,需加锁,而进程间的数据传递,仅是传递(数据共享)。
'''
# 写数据进程执行的代码:
def write(q):
    print('Process to write: %s' % os.getpid())
    for value in ['A', 'B', 'C']:
        print('Put %s to queue...' % value)
        q.put(value)  # 推送
        time.sleep(random.random())
# 读数据进程执行的代码:
def read(q):
    print('Process to read: %s' % os.getpid())
    while True:
        value = q.get(True)  # 获取
        print('Get %s from queue.' % value)
if __name__=='__main__':
    # 父进程创建Queue,并传给各个子进程:
    q = Queue()
    pw = Process(target=write, args=(q,))
    pr = Process(target=read, args=(q,))
    # 启动子进程pw,写入:
    pw.start()
    # 启动子进程pr,读取:
    pr.start()
    # 等待pw结束:
    pw.join()
    # pr进程里是死循环,无法等待其结束,只能强行终止:
    pr.terminate()
    
    
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Changhua Gong
from multiprocessing import Process, Pipe
from time import sleep
from multiprocessing import freeze_support
'''
def Pipe(duplex=True):
    return Connection(), Connection()
说明个问题:Pipe仅能两个进程间的交互,类似电话线形式收发
'''
def run_A(conn):
    for i in range(3):
        conn.send("I am from run_A: %s" % i)
        sleep(0.3)
    for i in range(5):
        print(conn.recv())
def run_B(conn):
    for i in range(3):
        print(conn.recv())
    for i in range(5):
        conn.send("I am from run_B: %s" %  i)
        sleep(0.3)
if __name__ == "__main__":
    freeze_support()
    conn1, conn2 = Pipe()
    pA = Process(target=run_A, args=(conn1,))
    pB = Process(target=run_B, args=(conn2,))
    pA.start()
    pB.start()
    pA.join()
    pB.join()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档