Observers on multiprocessing

Question:

I am doing a GUI that requires of multiprocessing. So I have one process that is the GUI and another process that is running for ever and it’s a scheduler. They share the var scheduler (that keeps the events and manage them) by a manager.list().
When the GUI recibe an input from the user (click a button) it add an event on the scheduler and update this var for bouth processes.
On the other process that runs the scheduler events, when one event finish it also modify the var scheduler.
My problem is that when it happens I need to get a notification to the other process in order to reload the widgets. I was trying to implement an Observer but because of the multiprocessing I am not able to do so.

Do you know any way to do that?

Asked By: CrMatt9

||

Answers:

I have achive it by doing the next: -creating two manager.list one for sharing the scheduler var, the second one for sharing a boolean list.

Once the task is finish I add to the task a True. So by using the function refresh that check this manager.list() to find True inside. Once a True is found it reset the list and reload the widgets.

'''SUPPORT FUCTIONS TO TREAT THE MANAGER.LIST'''

def getupdate(schrec):
    if len(schrec)>0:
        return schrec[-1]


def loadupdate(schrec,sch):
    schrec[:]=[]
    schrec.append(sch)


def clear_fogt(ogtrec):
    ogtrec[:]=[]
    ogtrec.append(False)

'''WAITS UNTIL FIND True INSIDE ogtrec ( that is a manager.list() )'''

 def refresh():
        if True in ogtrec:
            clear_fogt(ogtrec)
            plot_scheduler()
        root.after(1000, refresh)
        print('refresh')


'''RUNS THE SCHEDULER'''

def sched_run(schrec,ogtrec):
    while True:
        #get last update of scheduler var
        scheduler = getupdate(schrec)

        while len(scheduler.og) == 0:
            time.sleep(0.5)
            #get last update of scheduler var
            scheduler = getupdate(schrec)

        print('Scheduler list not empty')

        #reload widg some bool change value
        ogtrec.append(True)

        #RUN
        scheduler.send_task_rob()
        print('Task is executing')
        print(simulation_robot_working(scheduler))
        time.sleep(simulation_robot_working(scheduler))
        scheduler.check_timing()
        print('Checked')

        #get last update of scheduler var
        scheduler = getupdate(schrec)
        scheduler.finish_og()
        print('OG updated')

        #update scheduler value
        loadupdate(schrec, scheduler)

        #reload widg
        ogtrec.append(True)
        print('finished')
        print(scheduler.og)
Answered By: CrMatt9

You can try this library https://600apples.github.io/dafi/. It works like observer as well

Answered By: gonzo