python 进程与线程的交互

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import time
import datetime
from multiprocessing import Process, Manager, Lock, Queue
from threading import Thread
import ctypes, os
import select

class Ethread(Thread):
    def __init__(self, dic, print1):
        super(Ethread, self).__init__()
        self.dict = dic
        self.print1 = print1

    def run(self):
        while True:
            if self.dict:
                self.print1()

class EPool(Process):
    def __init__(self):
        super(EPool, self).__init__()
        self.dict = {}

    def emit(self, msg):
        self.dict[msg] = msg

    def print1(self):
        print("%s" %self.dict)

    def run(self):
        self.thread = Ethread(self.dict, self.print1)
        self.thread.start()
        for i in range(10):
            self.emit(i)
            self.print1()

pool = EPool()
pool.start()

结果:
{0: 0}
{0: 0, 1: 1}
{0: 0, 1: 1, 2: 2}
{0: 0, 1: 1, 2: 2, 3: 3}
{0: 0, 1: 1, 2: 2, 3: 3, 4: 4}
{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5}
{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6}
{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7}
{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8}
{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}

也可以用 rabbitmq 进行交互:

recv.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import time
import datetime
from multiprocessing import Process, Manager, Lock
from Queue import Queue
from threading import Thread
import ctypes, os
import select
import pika

class Ethread(Thread):
    def __init__(self, dic, print1):
        super(Ethread, self).__init__()
        self.dict = dic
        self.print1 = print1
        self.cnt = 0

    def run(self):
        while True:
            if self.dict:
                self.print1()
                self.cnt = self.cnt + 1
                if self.cnt > 20:
                    break

class EPool(object):
    def __init__(self):
        self.dict = {}
        self.thread = Ethread(self.dict, self.print1)
        self.thread.start()

        # 连接 RabbitMQ server 
        connection = pika.BlockingConnection(
            pika.ConnectionParameters(host='localhost'))

        channel = connection.channel()

        # 如果生产者已经创建过了,则不会创建
        channel.queue_declare(queue='hello')

         # 告诉 RabbitMQ,callback 从 “hello” 队列接收消息
        channel.basic_consume(
            queue='hello', on_message_callback=self.callback, auto_ack=True)

        # 开启消费循环
        channel.start_consuming()     

    def print1(self):
        print("dict: %s" %self.dict)

    def emit(self, msg):
        self.dict[msg] = msg
        print('emit: %s' %self.dict)

    # 只要接收到消息,就会被 Pika 自动调用
    def callback(self, ch, method, properties, msg):
        self.emit(msg)
        print(' [*] Waiting for messages. To exit press CTRL+C')   

pool = EPool()

send.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import time
import datetime
from multiprocessing import Process, Manager, Lock
from Queue import Queue
from threading import Thread
import ctypes, os
import select
import pika

class EProcess(Process):
    def __init__(self):
        super(EProcess, self).__init__()
        # 连接 RabbitMQ server 
        self.connection = pika.BlockingConnection(
            pika.ConnectionParameters(host='localhost'))

        self.channel = self.connection.channel()

        # 创建一个名为 hello 的队列
        self.channel.queue_declare(queue='hello')
        
    def run(self):
        for i in range(10):
            print(" [x] Sent %s" %i)
            # 消息先到 exchange,再到 queue。exchange 为空字符串,说明使用默认的 exchange,这样就可以直接指明 queue 了
            # routing_key 指明 queue
            self.channel.basic_publish(exchange='', routing_key='hello', body=str(i))
        self.connection.close()

process = EProcess()
process.start()

结果:

python recv.py
emit: {'0': '0'}dict: {'0': '0'}

 [*] Waiting for messages. To exit press CTRL+C
dict: {'0': '0'}
dict: {'0': '0'}
dict: {'0': '0'}
dict: {'0': '0'}
dict: {'0': '0'}
dict: {'0': '0'}
dict: {'0': '0'}
dict: {'0': '0'}
 dict: {'1': '1', '0': '0'}
emit: {'1': '1', '0': '0'}
 [*] Waiting for messages. To exit press CTRL+C
dict: {'1': '1', '0': '0'}
emit: {'1': '1', '0': '0', '2': '2'}
 [*] Waiting for messages. To exit press CTRL+C
dict: {'1': '1', '0': '0', '2': '2'}
dict: {'1': '1', '0': '0', '3': '3', '2': '2'}
emit: {'1': '1', '0': '0', '3': '3', '2': '2'}
 [*] Waiting for messages. To exit press CTRL+C
dict: {'1': '1', '0': '0', '3': '3', '2': '2'}
 dict: {'1': '1', '0': '0', '3': '3', '2': '2', '4': '4'}
emit: {'1': '1', '0': '0', '3': '3', '2': '2', '4': '4'}
 [*] Waiting for messages. To exit press CTRL+C
emit: {'1': '1', '0': '0', '3': '3', '2': '2', '5': '5', '4': '4'}
 [*] Waiting for messages. To exit press CTRL+C
emit: {'1': '1', '0': '0', '3': '3', '2': '2', '5': '5', '4': '4', '6': '6'}
dict: {'1': '1', '0': '0', '3': '3', '2': '2', '5': '5', '4': '4', '6': '6'}
 [*] Waiting for messages. To exit press CTRL+C
emit: {'1': '1', '0': '0', '3': '3', '2': '2', '5': '5', '4': '4', '7': '7', '6': '6'}
 [*] Waiting for messages. To exit press CTRL+C
emit: {'1': '1', '0': '0', '3': '3', '2': '2', '5': '5', '4': '4', '7': '7', '6': '6', '8': '8'}
 [*] Waiting for messages. To exit press CTRL+C
dict: {'1': '1', '0': '0', '3': '3', '2': '2', '5': '5', '4': '4', '7': '7', '6': '6', '8': '8'}
 dict: {'1': '1', '0': '0', '3': '3', '2': '2', '5': '5', '4': '4', '7': '7', '6': '6', '9': '9', '8': '8'}
emit: {'1': '1', '0': '0', '3': '3', '2': '2', '5': '5', '4': '4', '7': '7', '6': '6', '9': '9', '8': '8'}
dict: {'1': '1', '0': '0', '3': '3', '2': '2', '5': '5', '4': '4', '7': '7', '6': '6', '9': '9', '8': '8'}
[*] Waiting for messages. To exit press CTRL+C
dict: {'1': '1', '0': '0', '3': '3', '2': '2', '5': '5', '4': '4', '7': '7', '6': '6', '9': '9', '8': '8'}
dict: {'1': '1', '0': '0', '3': '3', '2': '2', '5': '5', '4': '4', '7': '7', '6': '6', '9': '9', '8': '8'}
python send.py
 [x] Sent 0
 [x] Sent 1
 [x] Sent 2
 [x] Sent 3
 [x] Sent 4
 [x] Sent 5
 [x] Sent 6
 [x] Sent 7
 [x] Sent 8
 [x] Sent 9

你可能感兴趣的:(Python)