Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with multiprocessing
#1
I am fairly new to Python, but have run into a snag that I can't get past. I am trying to share a string variable between two processes. One process is capturing mqtt messages. I want the other
process to manipulate that data that is passed. Sharing data between these two processes has proven very difficult. Can you help? Thanks...
import multiprocessing
import time
import paho.mqtt.client as mqtt
import queue
q=queue.Queue()

def main2():
    data = "start in main2"
    print(data)
    q.put(data)

def do_mqtt():
    client = mqtt.Client()
    client.on_connect = on_connect
    client.on_message = on_message
    client.connect("192.168.1.31", 1883)
    client.loop_forever()
    
def on_connect(client, userdata, flags,rc):
    print("Connected with Code:" +str(rc))
    # Subscribe Topic
    client.subscribe("mqtt/master/#")

def on_message( client, userdata, msg):
    print ( str(msg.payload))
    data = "on_message:" +str(msg.payload)
    print(data)
    q.put(1,data)
    
def do_it():
    print("Number of cpu : ", multiprocessing.cpu_count())
    while q:
        print(q.get())
    
if __name__ == "__main__":
   
    p1 = multiprocessing.Process(target=do_it(), args=())
    p2 = multiprocessing.Process(target=do_mqtt(), args=())
    p1.start()
    p2.start()
    p1.join()
    p2.join()
    print("starting Main ")
    while q:
        print(q.get())
Reply


Messages In This Thread
Help with multiprocessing - by dsc77 - Nov-12-2019, 07:53 PM
RE: Help with multiprocessing - by stullis - Nov-12-2019, 10:50 PM
RE: Help with multiprocessing - by dsc77 - Nov-13-2019, 05:56 PM
RE: Help with multiprocessing - by stullis - Nov-13-2019, 08:59 PM
RE: Help with multiprocessing - by dsc77 - Nov-15-2019, 11:20 PM
RE: Help with multiprocessing - by stullis - Nov-19-2019, 01:17 AM
RE: Help with multiprocessing - by wrongfuloyster - Apr-07-2020, 04:56 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020