Python multiprocessing? I want to know how the code works.

Asked 2 years ago, Updated 2 years ago, 12 views

import time
import multiprocessing as mp
import wx

def myWorker(a, b):
    time.sleep(3)
    print('{} * {} = = {}'.format(a, b, a*b))

def onProcess(event):
    jobs = mp.cpu_count() * 2
    a = 5
    b = 10
    for job in range(jobs):
        mp.Process(target = myWorker, args = (a, b,)).start()

def onGUI(event):
    print('GUI is not blocked')

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
       wx.Frame.__init__(self, parent, id, title)
       panel = wx.Panel(self, wx.ID_ANY)
       sizer = wx.BoxSizer(wx.VERTICAL)
       gui_proc_btn = wx.Button(panel, wx.ID_ANY, 'GUI Process')
       other_proc_btn = wx.Button(panel, wx.ID_ANY, 'Other process')

       gui_proc_btn.Bind(wx.EVT_BUTTON, onGUI)
       sizer.Add(gui_proc_btn, 0, wx.ALL, 5)
       other_proc_btn.Bind(wx.EVT_BUTTON, onProcess)
       sizer.Add(other_proc_btn, 0, wx.ALL, 5)
       panel.SetSizer(sizer)

class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, 'test.py')
        frame.Show(True)
        return True

if __name__ == '__main__':
    app = MyApp()
    app.MainLoop()

If you execute the above code, there are two button icons. In the defonProcess(event) function, it is running multi-processing, and the target is a function that timeslip works, but when you press a button, the program does not stop and another button program runs I don't think it's because I gave you two classes. I've done a similar thing, but the program stops at a time slip... I wonder if the upper nose works without stopping.

import wx, time
import multiprocessing

class f(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        self.panel = wx.Panel(self)

        self.button = wx.Button(self.panel, -1, "click", pos=(100,100))
        self.button.Bind(wx.EVT_BUTTON, self.proccessing, id=-1)

    def proccessing(self, e):
        multiprocessing.Process(target=self.timesleep()).start()

    def timesleep(self):
        time.sleep(3)
        for i in range(10, 20):
            print(i)

if __name__ == '__main__':
    app = wx.App()
    fr = f()
    fr.Show(True)
    app.MainLoop()

python

2022-09-22 10:46

2 Answers

multiprocessing.Process(target=self.timesleep()).start() 

In the target section, try self.timesleep instead of self.timesleep().

multiprocessing.Process(target=self.timesleep).start() 

If you set target=self.timesleep(), the timesleep() method is invoked at that moment. That's why you're sleeping.

The target factor must pass the ID value of the method.


2022-09-22 10:46

Thank you for your reply.

import wx, time
import multiprocessing

def proccessing(event):
    multiprocessing.Process(target=timesleep).start()

def timesleep():
    time.sleep(3)
    for i in range(10, 20):
        print(i)

class f(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        self.panel = wx.Panel(self)
        self.button = wx.Button(self.panel, -1, "click", pos=(100,100))
        self.button.Bind(wx.EVT_BUTTON, proccessing, id=-1)

if __name__ == '__main__':
    app = wx.App()
    fr = f()
    fr.Show(True)
    app.MainLoop()

I can change it as you said. You can't put a function in the class... This is probably because it's connected as one in the class, right?


2022-09-22 10:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.