Python Forum

Full Version: help with multiprocess concept
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi im trying to write a module that communicates with a ucontroller via pyserial, plots data if needed. The general outline of what I want to do is in this Image.. but I`m having a hard time understanding how to get different processes to interact. for example in the pyseral module there are functions how can i call these functions to different processes?
If you are using Tkinter to plot the data, then multiprocessing is not necessary. You use the after() function to call a function periodically that gets the info from pyserial and plots the data.
thanks, but I'm not planning on using tkinter( i kind of hated using it) i'm sticking to just text based commands and a python script. also plotting may or may not be done, since it would have to depend on what is being called. Also the pyserial part has priority so i dont want anything to slow it down.

here is the solution found on stackoverflow from the user Carcigenicate

from operator import methodcaller

class Spawn:
    def __init__(self, _number, _max):
        self._number = _number
        self._max = _max
        # Don't call update here

    def request(self, x):
        print("{} was requested.".format(x))

    def update(self):
        while True:
            print("Spawned {} of {}".format(self._number, self._max))
            sleep(2)

if __name__ == '__main__':
    spawn = Spawn(1, 1)  # Create the object as normal

    p = multiprocessing.Process(target=methodcaller("update"), args=(spawn,)) # Run the loop in the process
    p.start()

    spawn.request(2)  # Now you can reference the "spawn" object to do whatever you like