Python Endpoint message - socket client 多进程并发

该博客介绍了如何使用Python的multiprocessing模块实现并发发送socket消息到iOS和Android设备。通过创建多进程池,分别处理iOS和Android的推送请求,每个请求包含多个设备和自定义消息。同时,它还记录了发送成功和失败的计数,存储在配置文件中。
摘要由CSDN通过智能技术生成
import multiprocessing
import time, datetime
import json
import string, socket
import fcntl
import ConfigParser


RES = [0, 0]


def lock_file(file):
fcntl.flock(file.fileno(), fcntl.LOCK_EX)


def unlock_file(file):
fcntl.flock(file.fileno(), fcntl.LOCK_UN)


def count_msg(name, is_success):
"""
Count and record msgs in configuration file according to the given name.
"""


sendmsg_ini = 'endpoint_sendmsg.ini'
conf = ConfigParser.SafeConfigParser()
# create configuration file if it does not exist
try:
file = open(sendmsg_ini, 'r+')
except IOError, e:
file = open(sendmsg_ini, 'a+')


lock_file(file)
conf.read(sendmsg_ini)
try:
total_success = conf.getint(name, '%s_send_success_counter' % name)
total_fail = conf.getint(name, '%s_send_failure_counter' % name)
except ConfigParser.NoSectionError, e:
# empty file, do init
print 'section %s does not exist in endpoint send msg configuration file, init section %s' % (name, name)
conf.add_section(name)
conf.set(name, '%s_send_success_counter' % name, '0')
conf.set(name, '%s_send_failure_counter' % name, '0')
if is_success:
conf.set(name, '%s_send_success_counter' % name, '1')
conf.set(name, '%s_send_failure_counter' % name, '0')
print 'total %s success task number is: %s' % (name, '1')
print 'total %s failure task number is: %s' % (name, '0')
else:
conf.set(name, '%s_send_failure_counter' % name, '1')
conf.set(name, '%s_send_success_counter' % name, '0')
print 'total %s success task number is: %s' % (name, '0')
print 'total %s failure task number is: %s' % (name, '1')
conf.write(file)
except Exception, e:
print 'write %s send message record error! message: %s' % (name, repr(e))
else:
if is_success:
total_success += 1
else:
total_fail += 1


conf.set(name, '%s_send_success_counter' % name, str(total_success))
conf.set(name, '%s_send_failure_counter' % name, str(total_fail))
conf.write(file)
print 'total %s success task number is: %s' % (name, total_success)
print 'total %s failure task number is: %s' % (name, total_fail)
finally:
file.flush()
unlock_file(file)
file.close()


ios_req1 = {
"messages": [
{
 "devicetoken": "d7c49bb6fab13bd338928dcfddd300a9e3ce27ca718eb07df0812f311afab2ed",
 "message": "Your ios app1 has been blocked by websense",
 "platform": 1
}
],
"length": 1
}
  
ios_req10 = {
"messages": [
{
 "devicetoken": "d7c49bb6fab13bd338928dcfddd300a9e3ce27ca718eb07df0812f311afab2ed",
 "message": "Your ios app1 has been blocked by websense",
 "platform": 1
},
{
 "devicetoken": "d7c49bb6fab13bd338928dcfddd300a9e3ce27ca718eb07df0812f311afab2ed",
 "message": "Your ios app2 has been blocked by websense",
 "platform": 1
},
{
 "devicetoken": "d7c49bb6fab13bd338928dcfddd300a9e3ce27ca718eb07df0812f311afab2ed",
 "message": "Your ios app3 has been blocked by websense",
 "platform": 1
},
{
 "devicetoken": "d7c49bb6fab13bd338928dcfddd300a9e3ce27ca718eb07df0812f311afab2ed",
 "message": "Your ios app4 has been blocked by websense",
 "platform": 1
},
{
 "devicetoken": "d7c49bb6fab13bd338928dcfddd300a9e3ce27ca718eb07df0812f311afab2ed",
 "message": "Your ios app5 has been blocked by websense",
 "platform": 1
},
{
 "devicetoken": "d7c49bb6fab13bd338928dcfddd300a9e3ce27ca718eb07df0812f311afab2ed",
 "message": "Your ios app6 has been blocked by websense",
 "platform": 1
},
{
 "devicetoken": "d7c49bb6fab13bd338928dcfddd300a9e3ce27ca718eb07df0812f311afab2ed",
 "message": "Your ios app7 has been blocked by websense",
 "platform": 1
},
{
 "devicetoken": "d7c49bb6fab13bd338928dcfddd300a9e3ce27ca718eb07df0812f311afab2ed",
 "message": "Your ios app8 has been blocked by websense",
 "platform": 1
},
{
 "devicetoken": "d7c49bb6fab13bd338928dcfddd300a9e3ce27ca718eb07df0812f311afab2ed",
 "message": "Your ios app9 has been blocked by websense",
 "platform": 1
},
{
 "devicetoken": "d7c49bb6fab13bd338928dcfddd300a9e3ce27ca718eb07df0812f311afab2ed",
 "message": "Your ios app10 has been blocked by websense",
 "platform": 1
}
],
"length": 10
}
  
ios_req2 = {
"messages": [
{
 "devicetoken": "d7c49bb6fab13bd338928dcfddd300a9e3ce27ca718eb07df0812f311afab2ed",
 "message": "Your ios app2 has been blocked by websense",
 "platform": 1,
 "customed_payload":{}
},
{
 "devicetoken": "d7c49bb6fab13bd338928dcfddd300a9e3ce27ca718eb07df0812f311afab2ed",
 "message": "Your ios app2.1 has been blocked by websense",
 "platform": 1,
 "customed_payload":{}
}
],
"length": 2
}
  
ios_req3 = {
"messages": [
{
 "devicetoken": "99999999",
 "message": "Your ios app1.3 has been blocked by websense",
 "platform": 1
},
{
 "devicetoken": "88888888",
 "message": "Your ios app1.4 has been blocked by websense",
 "platform": 1
}
],
"length": 2
}


ios_req4 = {"messages": 12345}
  
ard_req1 = {
"messages": [
{
 "devicetoken": "APA91bGKie7RpFTL6eG-3CVQb335ZnyFN2MChP9M_LlML_zu6wH8gV1MlhVkwXjQy1MpbWvuzybPwvxXmTC2FJuabSaEn_EbftRst3cfS992m4weSFEA66ddsSHTwh0f6pVO0R96Xi4-iBueS199Qt1jw-9Z6aHpUSdDp_0ySrucA9uosbBV8k8",
 "message": "Your ard app1 has been blocked by websense",
 "platform": 2,
 "customed_payload":{}
}
],
"length": 1
}


ard_req10 = {
"messages": [
{
 "devicetoken": "APA91bGKie7RpFTL6eG-3CVQb335ZnyFN2MChP9M_LlML_zu6wH8gV1MlhVkwXjQy1MpbWvuzybPwvxXmTC2FJuabSaEn_EbftRst3cfS992m4weSFEA66ddsSHTwh0f6pVO0R96Xi4-iBueS199Qt1jw-9Z6aHpUSdDp_0ySrucA9uosbBV8k8",
 "message": "Your ard app1 has been blocked by websense",
 "platform": 2,
 "customed_payload":{}
},
{
 "devicetoken": "APA91bGKie7RpFTL6eG-3CVQb335ZnyFN2MChP9M_LlML_zu6wH8gV1MlhVkwXjQy1MpbWvuzybPwvxXmTC2FJuabSaEn_EbftRst3cfS992m4weSFEA66ddsSHTwh0f6pVO0R96Xi4-iBueS199Qt1jw-9Z6aHpUSdDp_0ySrucA9uosbBV8k8",
 "message": "Your ard app2 has been blocked by websense",
 "platform": 2,
 "customed_payload":{}
},
{
 "devicetoken": "APA91bGKie7RpFTL6eG-3CVQb335ZnyFN2MChP9M_LlML_zu6wH8gV1MlhVkwXjQy1MpbWvuzybPwvxXmTC2FJuabSaEn_EbftRst3cfS992m4weSFEA66ddsSHTwh0f6pVO0R96Xi4-iBueS199Qt1jw-9Z6aHpUSdDp_0ySrucA9uosbBV8k8",
 "message": "Your ard app3 has been blocked by websense",
 "platform": 2,
 "customed_payload":{}
},
{
 "devicetoken": "APA91bGKie7RpFTL6eG-3CVQb335ZnyFN2MChP9M_LlML_zu6wH8gV1MlhVkwXjQy1MpbWvuzybPwvxXmTC2FJuabSaEn_EbftRst3cfS992m4weSFEA66ddsSHTwh0f6pVO0R96Xi4-iBueS199Qt1jw-9Z6aHpUSdDp_0ySrucA9uosbBV8k8",
 "message": "Your ard app4 has been blocked by websense",
 "platform": 2,
 "customed_payload":{}
},
{
 "devicetoken": "APA91bGKie7RpFTL6eG-3CVQb335ZnyFN2MChP9M_LlML_zu6wH8gV1MlhVkwXjQy1MpbWvuzybPwvxXmTC2FJuabSaEn_EbftRst3cfS992m4weSFEA66ddsSHTwh0f6pVO0R96Xi4-iBueS199Qt1jw-9Z6aHpUSdDp_0ySrucA9uosbBV8k8",
 "message": "Your ard app5 has been blocked by websense",
 "platform": 2,
 "customed_payload":{}
},
{
 "devicetoken": "APA91bGKie7RpFTL6eG-3CVQb335ZnyFN2MChP9M_LlML_zu6wH8gV1MlhVkwXjQy1MpbWvuzybPwvxXmTC2FJuabSaEn_EbftRst3cfS992m4weSFEA66ddsSHTwh0f6pVO0R96Xi4-iBueS199Qt1jw-9Z6aHpUSdDp_0ySrucA9uosbBV8k8",
 "message": "Your ard app6 has been blocked by websense",
 "platform": 2,
 "customed_payload":{}
},
{
 "devicetoken": "APA91bGKie7RpFTL6eG-3CVQb335ZnyFN2MChP9M_LlML_zu6wH8gV1MlhVkwXjQy1MpbWvuzybPwvxXmTC2FJuabSaEn_EbftRst3cfS992m4weSFEA66ddsSHTwh0f6pVO0R96Xi4-iBueS199Qt1jw-9Z6aHpUSdDp_0ySrucA9uosbBV8k8",
 "message": "Your ard app7 has been blocked by websense",
 "platform": 2,
 "customed_payload":{}
},
{
 "devicetoken": "APA91bGKie7RpFTL6eG-3CVQb335ZnyFN2MChP9M_LlML_zu6wH8gV1MlhVkwXjQy1MpbWvuzybPwvxXmTC2FJuabSaEn_EbftRst3cfS992m4weSFEA66ddsSHTwh0f6pVO0R96Xi4-iBueS199Qt1jw-9Z6aHpUSdDp_0ySrucA9uosbBV8k8",
 "message": "Your ard app8 has been blocked by websense",
 "platform": 2,
 "customed_payload":{}
},
{
 "devicetoken": "APA91bGKie7RpFTL6eG-3CVQb335ZnyFN2MChP9M_LlML_zu6wH8gV1MlhVkwXjQy1MpbWvuzybPwvxXmTC2FJuabSaEn_EbftRst3cfS992m4weSFEA66ddsSHTwh0f6pVO0R96Xi4-iBueS199Qt1jw-9Z6aHpUSdDp_0ySrucA9uosbBV8k8",
 "message": "Your ard app9 has been blocked by websense",
 "platform": 2,
 "customed_payload":{}
},
{
 "devicetoken": "APA91bGKie7RpFTL6eG-3CVQb335ZnyFN2MChP9M_LlML_zu6wH8gV1MlhVkwXjQy1MpbWvuzybPwvxXmTC2FJuabSaEn_EbftRst3cfS992m4weSFEA66ddsSHTwh0f6pVO0R96Xi4-iBueS199Qt1jw-9Z6aHpUSdDp_0ySrucA9uosbBV8k8",
 "message": "Your ard app10 has been blocked by websense",
 "platform": 2,
 "customed_payload":{}
}
],
"length": 10
}


ard_req2 = {
"messages": [
{
 #"devicetoken": "APA91bGTwkASQ2Oh9_TuwRUv87ptSnD2TlIUGvsWplo5_fUXXLZd8sM_6pn6CTXO7-MQhQHmJLv2nY2NotvRKPYzI50-v9kozpNjzDVKbqtXxF9LFNbBnEAVPP4_XigQ3PBTqPUoU8sZvlJ16c1KjuQK4La-qElBS5MtjqgIUtM1Qy74-zdGSBw",
 #"devicetoken": "APA91bFj7HIXCNQ1dlI-uun88UDxZjvVAOTRuwA1f9qTmAAdFTppp5v2rnTRRwWO6xD89OTJ3u0YzMurs25fdXWpS9wJBw7LACrYbA9AZBaPENFZdBNWdPDnDAI0e06KG77LTUVWWw-ftuTeSaD-_BjewLNZxNr_pmbawiQZ8L4GCNcnHlCC-2U",
 "devicetoken": "APA91bGKie7RpFTL6eG-3CVQb335ZnyFN2MChP9M_LlML_zu6wH8gV1MlhVkwXjQy1MpbWvuzybPwvxXmTC2FJuabSaEn_EbftRst3cfS992m4weSFEA66ddsSHTwh0f6pVO0R96Xi4-iBueS199Qt1jw-9Z6aHpUSdDp_0ySrucA9uosbBV8k8",
 "message": "Your ard app2 has been blocked by websense",
 "platform": 2,
 "customed_payload":{}
},
{
 "devicetoken": "APA91bGKie7RpFTL6eG-3CVQb335ZnyFN2MChP9M_LlML_zu6wH8gV1MlhVkwXjQy1MpbWvuzybPwvxXmTC2FJuabSaEn_EbftRst3cfS992m4weSFEA66ddsSHTwh0f6pVO0R96Xi4-iBueS199Qt1jw-9Z6aHpUSdDp_0ySrucA9uosbBV8k8",
 #"devicetoken": "APA91bFj7HIXCNQ1dlI-uun88UDxZjvVAOTRuwA1f9qTmAAdFTppp5v2rnTRRwWO6xD89OTJ3u0YzMurs25fdXWpS9wJBw7LACrYbA9AZBaPENFZdBNWdPDnDAI0e06KG77LTUVWWw-ftuTeSaD-_BjewLNZxNr_pmbawiQZ8L4GCNcnHlCC-2U",
 "message": "Your ard app2.1 has been blocked by websense",
 "platform": 2,
 "customed_payload":{}
},
{
 "devicetoken": "APA91bGKie7RpFTL6eG-3CVQb335ZnyFN2MChP9M_LlML_zu6wH8gV1MlhVkwXjQy1MpbWvuzybPwvxXmTC2FJuabSaEn_EbftRst3cfS992m4weSFEA66ddsSHTwh0f6pVO0R96Xi4-iBueS199Qt1jw-9Z6aHpUSdDp_0ySrucA9uosbBV8k8",
 #"devicetoken": "APA91bFj7HIXCNQ1dlI-uun88UDxZjvVAOTRuwA1f9qTmAAdFTppp5v2rnTRRwWO6xD89OTJ3u0YzMurs25fdXWpS9wJBw7LACrYbA9AZBaPENFZdBNWdPDnDAI0e06KG77LTUVWWw-ftuTeSaD-_BjewLNZxNr_pmbawiQZ8L4GCNcnHlCC-2U",
 "message": "Your ard app2.2 has been blocked by websense",
 "platform": 2,
 "customed_payload":{}
},
{
 "devicetoken": "APA91bGKie7RpFTL6eG-3CVQb335ZnyFN2MChP9M_LlML_zu6wH8gV1MlhVkwXjQy1MpbWvuzybPwvxXmTC2FJuabSaEn_EbftRst3cfS992m4weSFEA66ddsSHTwh0f6pVO0R96Xi4-iBueS199Qt1jw-9Z6aHpUSdDp_0ySrucA9uosbBV8k8",
 #"devicetoken": "APA91bFj7HIXCNQ1dlI-uun88UDxZjvVAOTRuwA1f9qTmAAdFTppp5v2rnTRRwWO6xD89OTJ3u0YzMurs25fdXWpS9wJBw7LACrYbA9AZBaPENFZdBNWdPDnDAI0e06KG77LTUVWWw-ftuTeSaD-_BjewLNZxNr_pmbawiQZ8L4GCNcnHlCC-2U",
 "message": "Your ard app2.3 has been blocked by websense",
 "platform": 2,
 "customed_payload":{}
}
],
"length": 4
}
  
  
ard_req3 = {
"messages": [
{
 "devicetoken": "APA91bGKie7RpFTL6eG-3CVQb335ZnyFN2MChP9M_LlML_zu6wH8gV1MlhVkwXjQy1MpbWvuzybPwvxXmTC2FJuabSaEn_EbftRst3cfS992m4weSFEA66ddsSHTwh0f6pVO0R96Xi4-iBueS199Qt1jw-9Z6aHpUSdDp_0ySrucA9uosbBV8k8",
 "message": "Your ard app3.1 has been blocked by websense",
 "platform": 2,
 "customed_payload":{}
},
{
 "devicetoken": "APA91bGTwkASQ2Oh9",
 "message": "Your ard app3.2 has been blocked by websense",
 "platform": 2,
 "customed_payload":{}
}
],
"length": 2
}
  
ard_req4 = {
"messages": [
{
 "devicetoken": "9999999",
 "message": "Your ard app4.1 has been blocked by websense",
 "platform": 2,
 "customed_payload":{}
}
],
"length": 1
}
def func(msg):
#for i in xrange(3):
# print msg
# time.sleep(1)
return "done " + str(msg)


def sendmsg_ios(md, x):  
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)  
sock.connect("/var/run/mdmi/push_service.sock")  
ios_req10["messages"][3]["message"] = "Your ios app %d has been blocked by websense - %s" % (x, md)
#ios_req10["messages"][3]["message"] += " - "
#ios_req10["messages"][3]["message"] += str(md)
sock.send(json.dumps(ios_req10))
results = sock.recv(4096)
sock.close()  
print results
res = json.loads(results)
code = res["code"] 
try:
if (code==200):
count_msg('APN', True)
else:
count_msg('APN', False)
except Exception, e:
print "Exception: %s" % str(e)
return code
  
def sendmsg_ard(md, x):  
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)  
sock.connect("/var/run/mdmi/push_service.sock")  
ard_req10["messages"][0]["message"] = "Your ard app %d has been blocked by websense - %s" % (x, md)
#ard_req4["messages"][0]["message"] += " - "
#ard_req4["messages"][0]["message"] += str(md)
sock.send(json.dumps(ard_req10))
results = sock.recv(4096)
sock.close()  
print results
res = json.loads(results)
code = res["code"] 
try:
if (code==200):
count_msg('GCM', True)
else:
count_msg('GCM', False)
except Exception, e:
print "Exception: %s" % str(e)
return code
  
if __name__ == "__main__":


while True:


ios_pool = multiprocessing.Pool(processes=10)
ard_pool = multiprocessing.Pool(processes=10)


result = []
starttime = time.time()
req_cnt = 30
for x in xrange(req_cnt):
md = datetime.datetime.now().microsecond
result.append(ios_pool.apply_async(sendmsg_ios, (md, x, )))
ios_pool.close()
ios_pool.join()
endtime = time.time()
sec_cost = endtime - starttime
output = "Sent %d iOS requests time cost: %d seconds.\n" % (req_cnt, sec_cost)
print output
print "Sub-process(es) done."
time.sleep(10)

result = []
starttime = time.time()
req_cnt = 30
for x in xrange(req_cnt):
md = datetime.datetime.now().microsecond
result.append(ard_pool.apply_async(sendmsg_ard, (md, x, )))
ard_pool.close()
ard_pool.join()
endtime = time.time()
sec_cost = endtime - starttime
output = "Sent %d android requests time cost: %d seconds.\n" % (req_cnt, sec_cost)
print output
print "Sub-process(es) done."
time.sleep(10)


del ios_pool
del ard_pool






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值