Python 다중 프로세스 통신 모듈

高洛峰
풀어 주다: 2016-10-18 10:00:27
원래의
1701명이 탐색했습니다.

다중 프로세스 통신 방법에는 여러 가지가 있습니다. 방금 Python으로 패키지된 다중 프로세스 통신 모듈 multiprocessing.connection을 사용해 보았습니다.

간단한 테스트 결과 효율성은 괜찮습니다. 소켓 캡슐화여야 합니다. 효율성은 4krps에 도달할 수 있으며 이는 많은 요구 사항을 충족할 수 있습니다.

첨부된 코드는 다음과 같습니다.

클라이언트

#!/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
로그인 후 복사

서버

#!/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)
로그인 후 복사


관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!