Python多進程通信例子

jopen 10年前發布 | 22K 次閱讀 Python Python開發

客戶端:

import webtul
from multiprocessing.connection import Client
  
= 0
try:
    while True:
        += 1
        address = ('10.33.41.112'6666)
        conn = Client(address, authkey='hellokey')
        #print conn.recv()
        = conn.recv()
        conn.close()
except:
    pass

 

服務器端:

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)
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!