Simple implementation of python multi-process communication module

Source: Internet
Author: User

Multi-process communication methods vary by number. Just now I tried to encapsulate the multi-process communication module multiprocessing. connection in python.

A simple test shows that the efficiency is good. It should be based on socket encapsulation. The efficiency can reach 4 krps, which can meet many requirements.

The Code is as follows:

Client

Copy codeThe Code is as follows:
#! /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 t:
Pass

Print

Server
Copy codeThe Code is as follows:
#! /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 t 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 Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.