这篇文章主要为大家详细介绍了python 多进程通信模块入门实例,具有一定的参考价值,可以用来参考一下。
对python这个高级语言感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!
多进程通信方法好多,不一而数。刚才试python封装好嘅多进程通信模块 multiprocessing.connection。
简单测试一下,效率还可以,应该系对socket封装,效率可以达到4krps,可以满足好多方面嘅需求啦。
附代码如下:
client
# @param python 多进程通信模块
# @author 四海网|www.q1010.com
#!/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
# End www_512pic_com
server
# @param python 多进程通信模块
# @author 四海网|www.q1010.com
#!/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)
# End www_512pic_com
本文来自:http://www.q1010.com/181/2279-0.html
注:关于python 多进程通信模块入门实例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:多进程,通信模块
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。