0

My problem is similar to what was asked here: Cannot 'pickle' a Tkinter object in a multiprocessing environment in Windows

Unfortunately i don't quite understand how to pass a target function into the subprocess class which is created. And if possible i would like to pass arguments and get returns of that function (optional). I basically just copied the code from the question above. Just for reference, this is the code: (not my code at all)

import multiprocessing
from tkinter import *
from queue import Empty


class Subprocess(multiprocessing.Process):
    def __init__(self):
        multiprocessing.Process.__init__(self)
        self.queue = multiprocessing.Queue()

    def run(self):
        self.root = Tk()
        self.root.after(100, self._check_queue)  # Check the queue every 100ms
        self.root.mainloop()

    def _check_queue(self):
        try:
            out = self.queue.get_nowait()
            if out == 'stop':
                self.do_stop()
                return
        except Empty:
            pass
        self.root.after(100, self._check_queue)

    def stop(self):
        self.queue.put('stop')

    def do_stop(self):
        self.root.destroy()
Honn
  • 737
  • 2
  • 18
  • This `Subprocess` class is built to run `tkinter` entirely in another `Process`. This leaves the main thread to do what it likes. You can still communicate with `tkinter` through the `queue` member. What target function did you have in mind and how would you want to pass it into this class? – quamrana Feb 06 '21 at 18:37
  • What kind of function do you want to pass? The linked question and code shows how to create a tkinter application in the other process. However the sample app essentially does nothing other than periodically check to see if the main process has told it to stop. – martineau Feb 06 '21 at 19:05
  • @quamrana I want to use this for my chess engine to keep the GUI responsive while the engine calculates the move. – Honn Feb 06 '21 at 19:18
  • Yes, you can use this class to run the GUI whilst your engine calculates chess. You can use queues to send messages back and forth. – quamrana Feb 06 '21 at 20:55
  • this example doesn't expect any function as example - it has all code in `run()` and if you want to run something different in subprocess then replace code in `run()`. And if you want to add function then add it directly in `run()`. This example works different then in normal situation - usually `GUI` works in main thread and `threads` or `multiporcessing` is used to run other code (ie. chess engine) in other thread/process. This example runs `GUI` in other thread/process so you can run other code (ie. chess engine) in main thread. – furas Feb 06 '21 at 21:56
  • please post the entire error message you're getting – Aaron Feb 07 '21 at 22:24
  • @Aaron I am not getting an error message. I just donb't know how to pass a target function into the class. – Honn Feb 11 '21 at 17:31
  • @martineau Sorry I didn't see your comment until now. I want to use this to let my chess engine calculate moves, while GUI is still responsive and updating (progress bar, etc.). I originally had it working with the `threading` module, which broke after some time. When I switched to `multiprocessing` i got this error (see linked question). – Honn Feb 11 '21 at 18:58
  • The linked question is not really a good template to follow for what you want to do. The `multiprocessing.Process` subclass defined isn't being passed a function to execute which is generally how the base class works, so I don't think you need to define your own subclass—just use the regular one. See [my answer](https://stackoverflow.com/a/53697547/355230) about doing something similar with threads (because the basic idea would work with processes, too). BTW, isn't Württemberg where Einstein was born? – martineau Feb 11 '21 at 19:23
  • @martineau Yes, indeed (specifically in Ulm). Thanks for your answer. I will try to get it to work with multiprocessing since my application is very CPU heavy. – Honn Feb 12 '21 at 07:17

0 Answers0