Python Forum
Multiprocessing share variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiprocessing share variable
#1
Hello!

I have the following problem :

Main.py:

import multiprocessing
import FilePlot
x = []
y = []

while True:
word = input()
if word == "plot":
multiprocessing.Process(target=FilePlot.runPlot).start()
if word == "data":
x = [1,2,3]
y = [1,2,3]
FilePlot.py:

import Main

import wx
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx as NavigationToolbar
import threading
from time import sleep

class PlotWindow(wx.Frame):
def __init__(self, *args, **kwds):
kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.SetSize((657, 548))

# Menu Bar
self.PlotMenuBar = wx.MenuBar()
wxglade_tmp_menu = wx.Menu()
self.PlotMenuBar.loadDataMenu = wxglade_tmp_menu.Append(wx.ID_ANY, "Load data to plot", "")
self.PlotMenuBar.saveImageMenu = wxglade_tmp_menu.Append(wx.ID_ANY, "Save plot image to file", "")
self.PlotMenuBar.Append(wxglade_tmp_menu, "File")
wxglade_tmp_menu = wx.Menu()
self.PlotMenuBar.logXmenu = wxglade_tmp_menu.Append(wx.ID_ANY, "Log(x)", "", wx.ITEM_CHECK)
self.PlotMenuBar.logYmenu = wxglade_tmp_menu.Append(wx.ID_ANY, "Log(y)", "", wx.ITEM_CHECK)
self.PlotMenuBar.absXmenu = wxglade_tmp_menu.Append(wx.ID_ANY, "Abs(x)", "", wx.ITEM_CHECK)
self.PlotMenuBar.absYmenu = wxglade_tmp_menu.Append(wx.ID_ANY, "Abs(y)", "", wx.ITEM_CHECK)
wxglade_tmp_menu.AppendSeparator()
self.PlotMenuBar.keepPlotMenu = wxglade_tmp_menu.Append(wx.ID_ANY, "Keep last measurement on plot", "", wx.ITEM_CHECK)
self.PlotMenuBar.clearLastMenu = wxglade_tmp_menu.Append(wx.ID_ANY, "Clear last loaded plot", "")
self.PlotMenuBar.Append(wxglade_tmp_menu, "Plot")
self.SetMenuBar(self.PlotMenuBar)
# Menu Bar end

self.__set_properties()
self.__do_layout()


def __set_properties(self):
self.SetTitle("PlotWindow")
self.SetBackgroundColour(wx.Colour(104, 104, 104))

self.Bind(wx.EVT_CLOSE, self.OnExit)


def __do_layout(self):
mainSizer = wx.BoxSizer(wx.VERTICAL)
secondSizer = wx.BoxSizer(wx.VERTICAL)
labelSizer = wx.BoxSizer(wx.HORIZONTAL)

self.plot_panel = MatplotPanel(self)

secondSizer.Add(self.plot_panel, 8, wx.ALL | wx.EXPAND, 0)



self.cursorCoordLabel = wx.StaticText(self, wx.ID_ANY, "x: None, y: None")
#self.cursorCoordLabel.SetBackgroundColour(wx.Colour(221, 221, 221))
self.cursorCoordLabel.SetForegroundColour(wx.Colour(255, 255, 255))
self.cursorCoordLabel.SetFont(wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, 0, ""))
self.cursorCoordLabel.SetToolTip("Cursor coordinates")
labelSizer.Add(self.cursorCoordLabel, 1, 0, 0)
self.distPlotLabel = wx.StaticText(self, wx.ID_ANY, "dx: None, dy: None")
#self.distPlotLabel.SetBackgroundColour(wx.Colour(50, 153, 204))
self.distPlotLabel.SetForegroundColour(wx.Colour(255, 255, 255))
self.distPlotLabel.SetFont(wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, 0, ""))
self.distPlotLabel.SetToolTip("Distances for x and y. Left click on plot for the first point and Right click on plot for the second point")
labelSizer.Add(self.distPlotLabel, 1, 0, 0)
secondSizer.Add(labelSizer, 0, wx.ALL | wx.EXPAND, 0)
fileNameLabel = wx.StaticText(self, wx.ID_ANY, "File name: None")
fileNameLabel.SetBackgroundColour(wx.Colour(94, 94, 94))
fileNameLabel.SetForegroundColour(wx.Colour(254, 254, 254))
fileNameLabel.SetFont(wx.Font(10, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, 0, ""))
fileNameLabel.SetToolTip("This represent the name of the file used to store measurement data")
secondSizer.Add(fileNameLabel, 0, wx.EXPAND, 0)
mainSizer.Add(secondSizer, 1, wx.EXPAND, 0)
self.SetSizer(mainSizer)
self.Layout()
self.Centre()

self.xi = []
self.yi = []
self.plot_panel.canvas.draw()

self.redraw_timer = wx.Timer(self) # foloseste timer pentru a updata plot-ul
self.Bind(wx.EVT_TIMER, self.on_redraw_timer, self.redraw_timer)# executa functia on_redraw_timer
self.redraw_timer.Start(100) # 100 ms

def on_redraw_timer(self,event):

self.plot_panel
self.xi = Main.x
self.yi = Main.y
self.plot_panel.plot_data.set_xdata(self.xi)
self.plot_panel.plot_data.set_ydata(self.yi)



self.plot_panel.axes.relim()
self.plot_panel.axes.autoscale_view()
self.plot_panel.canvas.draw()


def OnExit(self, event):
warning = wx.MessageBox("Are you sure you want to exit ?", \
'Atention', wx.YES_NO | wx.ICON_WARNING)
if warning == wx.YES:
self.redraw_timer.Stop()
self.Destroy()




class MatplotPanel(wx.Panel):

def __init__(self, parent):
wx.Panel.__init__(self, parent,-1,size=(657, 548))

self.figure = Figure(facecolor='white',dpi = 95)
self.axes = self.figure.add_subplot(111,facecolor='white')
self.figure.subplots_adjust(left=0.08, right=0.95, top=0.96, bottom=0.1)


self.canvas = FigureCanvas(self,-1,self.figure)

self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.canvas, -1, wx.EXPAND| wx.ALL)
self.SetSizer(self.sizer)

self.plot_data, = self.axes.plot([],[],"-o",color="coral",markersize=4, markeredgecolor='olive')


class MyApp(wx.App):
def OnInit(self):
self.frame = PlotWindow(None, wx.ID_ANY, "")
self.SetTopWindow(self.frame)
self.frame.Show()
return True


def runPlot():
app = MyApp(0)
app.MainLoop()
The problem is that x and y are not updated in the function bellow from FilePlot.py:
def on_redraw_timer(self,event):

self.plot_panel
self.xi = Main.x
self.yi = Main.y
self.plot_panel.plot_data.set_xdata(self.xi)
self.plot_panel.plot_data.set_ydata(self.yi)
I cannot pass x and y variables between processes. I read some documentation from here https://docs.python.org/3/library/multip...-processes but I don't understand much of it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Timestamp of file changes if a share mapped to a server…. tester_V 34 4,009 Jul-04-2023, 05:19 AM
Last Post: tester_V
  access share attributed among several class methods drSlump 0 1,072 Nov-18-2021, 03:02 PM
Last Post: drSlump
  How to share a numpy array between 2 processes on Windows? qstdy 0 2,171 Jan-29-2021, 04:24 AM
Last Post: qstdy
  Access Network SMB Share on a NAS deltapy 2 9,076 Oct-31-2020, 09:14 AM
Last Post: deltapy
  How I can use multiprocessing with upickled module variable? AlekseyPython 3 4,080 Oct-31-2019, 06:09 AM
Last Post: AlekseyPython
  SHARE PYTHON FILE ambush 1 2,861 Jan-28-2019, 04:04 AM
Last Post: snippsat
  Copy info of 2 files with share information AngelRuizM 1 2,684 Aug-14-2017, 07:31 PM
Last Post: buran
  Properly share logger objects CardBoy 2 3,181 Jun-27-2017, 08:33 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

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