分享

python 多进程通信模块

 xf_baby 2014-02-23
python 多进程通信模块的简单实现。
多进程通信方法好多,不一而数。刚才试python封装好嘅多进程通信模块 multiprocessing.connection。
简单测试咗一下,效率还可以,应该系对socket封装,效率可以达到4krps,可以满足好多方面嘅需求啦。
附代码如下:
client端:
#!/usr/bin/python  
  1. # -*- coding: utf-8 -*-  
  2. """ download - slave 
  3. """  
  4. __author__ = 'Zagfai'  
  5. __license__ = 'MIT@2014-02'  
  6. import webtul  
  7. from multiprocessing.connection import Client  
  8. a = 0  
  9. try:  
  10.     while True:  
  11.         a += 1  
  12.         address = ('10.33.41.112'6666)  
  13.         conn = Client(address, authkey='hellokey')  
  14.         #print conn.recv()  
  15.         d = conn.recv()  
  16.         conn.close()  
  17. except:  
  18.     pass  
  19. print a  
server端:
#!/usr/bin/python  
  1. # -*- coding: utf-8 -*-  
  2. """ downloader - master server 
  3. """  
  4. __author__ = 'Zagfai'  
  5. __license__ = 'MIT@2014-02'  
  6. import webtul  
  7. from multiprocessing.connection import Listener  
  8. from threading import Thread  
  9. #by www.jbxue.com  
  10.   
  11. def listener():  
  12.     address = ('10.33.41.112'6666)  
  13.     listener = Listener(address, backlog=100, authkey='hellokey')  
  14.     while True:  
  15.         conn = listener.accept()  
  16.         #print 'connection accepted from', listener.last_accepted  
  17.         try:  
  18.             conn.send({'1':2'2':'abc'})  
  19.         except Exception, e:  
  20.             print e  
  21.         finally:  
  22.             conn.close()  
  23.     listener.close()  
  24. listener_th = Thread(target=listener)  
  25. listener_th.daemon = True  
  26. listener_th.start()  
  27. listener_th.join(timeout=20)  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多