python 多进程队列数据处理
# -*- coding:utf8 -*-
import paho.mqtt.client as mqtt
from multiprocessing import Process, Queue
import time, random, os
import camera_person_num
MQTTHOST = "172.19.4.4"
MQTTPORT = 1883
mqttClient = mqtt.Client()
q = Queue()
# 连接MQTT服务器
def on_mqtt_connect():
mqttClient.connect(MQTTHOST, MQTTPORT, 60)
mqttClient.loop_start()
# 消息处理函数
def on_message_come(lient, userdata, msg):
# print(msg.topic + ":" + str(msg.payload.decode("utf-8")))
q.put(msg.payload.decode("utf-8")) # 放入队列
print("产生消息", msg.payload.decode("utf-8"))
# 消息处理开启多进程
# p = Process(target=talk, args=("/camera/person/num/result", msg.payload.decode("utf-8")))
# p.start()
def consumer(q, pid):
print("开启消费序列进程", pid)
while True:
msg = q.get()
# p = Process(target=talk, args=("/camera/person/num/result", msg, pid))
# p.start()
talk("/camera/person/num/result", msg, pid)
# subscribe 消息订阅
def on_subscribe():
mqttClient.subscribe("test123", 1) # 主题为"test"
mqttClient.on_message = on_message_come # 消息到来处理函数
# publish 消息发布
def on_publish(topic, msg, qos):
mqttClient.publish(topic, msg, qos);
# 多进程中发布消息需要重新初始化mqttClient
def talk(topic, msg, pid):
cameraPsersonNum = camera_person_num.CameraPsersonNum(msg)
t_max, t_mean, t_min = cameraPsersonNum.personNum()
# time.sleep(20)
print("消费消息", pid, msg)
mqttClient2 = mqtt.Client()
mqttClient2.connect(MQTTHOST, MQTTPORT, 60)
mqttClient2.loop_start()
mqttClient2.publish(topic, '{"max":' + str(t_max) + ',"mean":' + str(t_mean) + ',"min:"' + t_min + '}', 1)
mqttClient2.disconnect()
def main():
on_mqtt_connect()
on_subscribe()
for i in range(1, 3):
c1 = Process(target=consumer, args=(q, i))
c1.start()
while True:
pass
if __name__ == '__main__':
main()
python 多进程队列数据处理的更多相关文章
- Python多进程队列间传递对象
前言 在python 需要在队列中传递对象, 会出现进程不能正常退出的情况. 其原因是因为 在父进程 向子进程传入的Queue对象不对, Queue对象正常是子进程之间的信息传递, 而当我在父进程 创 ...
- python 并发编程 多进程 队列目录
python 并发编程 多进程 队列 python 并发编程 多进程 生产者消费者模型介绍 python 并发编程 多进程 生产者消费者模型总结 python 并发编程 多进程 JoinableQue ...
- Python多进程编程
转自:Python多进程编程 阅读目录 1. Process 2. Lock 3. Semaphore 4. Event 5. Queue 6. Pipe 7. Pool 序. multiproces ...
- Python多进程multiprocessing使用示例
mutilprocess简介 像线程一样管理进程,这个是mutilprocess的核心,他与threading很是相像,对多核CPU的利用率会比threading好的多. import multipr ...
- python 多进程开发与多线程开发
转自: http://tchuairen.blog.51cto.com/3848118/1720965 博文作者参考的博文: 博文1 博文2 我们先来了解什么是进程? 程序并不能单独运行,只有将程 ...
- day-4 python多进程编程知识点汇总
1. python多进程简介 由于Python设计的限制(我说的是咱们常用的CPython).最多只能用满1个CPU核心.Python提供了非常好用的多进程包multiprocessing,他提供了一 ...
- 【转】Python多进程编程
[转]Python多进程编程 序. multiprocessingpython中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程.Pytho ...
- Python 多进程multiprocessing
一.python多线程其实在底层来说只是单线程,因此python多线程也称为假线程,之所以用多线程的意义是因为线程不停的切换这样比串行还是要快很多.python多线程中只要涉及到io或者sleep就会 ...
- Python 多进程编程之 进程间的通信(在Pool中Queue)
Python 多进程编程之 进程间的通信(在Pool中Queue) 1,在进程池中进程间的通信,原理与普通进程之间一样,只是引用的方法不同,python对进程池通信有专用的方法 在Manager()中 ...
随机推荐
- go语言学习之从例子开始
[目录] go语言从例子开始之Example1.helloworld go语言从例子开始之Example2.类型 go语言从例子开始之Example3.变量 go语言从例子开始之Example4.常量 ...
- POJ 3237 树链剖分
题目链接:http://poj.org/problem?id=3237 题意:给定一棵n个结点n-1条边的树. 每条边都是一个边权. 现在有4种操作 1:CHANGE I V:把(输入的)第i条边的边 ...
- shlwapi.h文件夹文件是否存在
{ if( NULL == lpszFileName) { return FALSE; } if (PathFileExists(lpszFileName)) { return TRUE; } els ...
- Tab选项卡 自动切换效果js实现
try.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...
- restful风格接口类型和优点
从事web开发工作有一小段时间,REST风格的接口,这样的词汇总是出现在耳边,然后又没有完全的理解,您是不是有和我相同的疑问呢?那我们一起来一探究竟吧! 就是用URL定位资源,用HTTP描述操作. 知 ...
- firewall&iptables
一.firewall 查看firewall状态 firewall-cmd --state 如果firewall为关闭状态,先启动firewall systemctl start firewalld 添 ...
- boost compressedPair
boost::compressed_pair behaves like std::pair. However, if one or both template parameters are empty ...
- 【C++11新特性】 C++11智能指针之weak_ptr
如题,我们今天要讲的是C++11引入的三种智能指针中的最后一个:weak_ptr.在学习weak_ptr之前最好对shared_ptr有所了解.如果你还不知道shared_ptr是何物,可以看看我的另 ...
- MAX3232 每次只有在上电后,再连接串口线正常——保护电阻。RS232防雷保护
转载:http://m.newsmth.net/article/Circuit/298517?p=1 转载:http://www.360doc.com/content/18/0719/13/57938 ...
- 树结构遍历节点名字提取,这里提取的是el-tree数据结构,封装成函数
/** * 树形数据提取节点 * @param {*} data */ export function treeDataGetnode (data) { var res = [] var child= ...