简体   繁体   中英

Python multiprocessing share variable with wxApp

I have 2 files "console.py" and "gui.py". What I try to do is to start the GUI app located in "gui.py" from the "console.py" in a different process and to print the "a" variable from the "console.py" (which is continuously updated) in the GUI app. In other words, I try to share variables across 2 files in 2 different processes. I looked on multiprocessing documentation located here https://docs.python.org/2/library/multiprocessing.html but I dont know how to do it practically. Any kind of help is appreciated! Thank you!

console.py:

import multiprocessing
from time import sleep
import random
import gui

a = []

word = input(">>")
if "plot" in word:
    multiprocessing.Process(target=gui.runPlot).start()
    while True:
        a.append(random.randint(0,10))
        sleep(1)
            

gui.py:

import wx

class MyFrame(wx.Frame):    
    def __init__(self):
        super().__init__(parent=None, title='Hello World')
        self.Show()
        
        self.redraw_timer = wx.Timer(self) 
        self.Bind(wx.EVT_TIMER, self.on_redraw_timer, self.redraw_timer)
        self.redraw_timer.Start(100)

    def on_redraw_timer(self, event):
        #here I want to print the "a" variable from console.py
        print(console.a)

    
def runPlot():
    app = wx.App()
    frame = MyFrame()
    app.MainLoop()

You could try PyPubSub , when the variable a gets updated in console.py it can transmit it. A pubsub listener in gui.py will recieve the value and update accordingly, link: https://pypubsub.readthedocs.io/en/v4.0.3/usage/usage_basic.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM