A simple implementation of the python multiprocess communication module

  • 2020-04-02 13:26:16
  • OfStack

Multiprocess communication methods are numerous, not to mention numerous. Just try python package � good multi-process communication module multiprocessing. The connection.

Simple test, efficiency can also be, should be on the socket package, efficiency can reach 4krps, can meet the needs of many aspects of the.

The attached code is as follows:

The client


#!/usr/bin/python
# -*- coding: utf-8 -*-
""" download - slave
"""
__author__ = 'Zagfai'
__license__ = 'MIT@2014-02'
import webtul
from multiprocessing.connection import Client
a = 0
try:
    while True:
        a += 1
        address = ('10.33.41.112', 6666)
        conn = Client(address, authkey='hellokey')
        #print conn.recv()
        d = conn.recv()
        conn.close()
except:
    pass
print a

server


#!/usr/bin/python
# -*- coding: utf-8 -*-
""" downloader - master server
"""
__author__ = 'Zagfai'
__license__ = 'MIT@2014-02'
import webtul
from multiprocessing.connection import Listener
from threading import Thread

def listener():
    address = ('10.33.41.112', 6666)
    listener = Listener(address, backlog=100, authkey='hellokey')
    while True:
        conn = listener.accept()
        #print 'connection accepted from', listener.last_accepted
        try:
            conn.send({'1':2, '2':'abc'})
        except Exception, e:
            print e
        finally:
            conn.close()
    listener.close()
listener_th = Thread(target=listener)
listener_th.daemon = True
listener_th.start()
listener_th.join(timeout=20)


Related articles: