1

Im planning to create a chatbot for Twitch.tv in Python. Id like to use multiple processes for the different main functionalities (one for the bot itself, and one for the GUI). Every process should have a reference to the other processes in form of a variable inside them. I havent started coding the bot itself, but i tested the multiprocessing with a small example.

Main file:

from BotProcess import botProcess
from GuiProcess import guiProcess

botProcess = botProcess()
guiProcess = guiProcess()
botProcess.guiProcess = guiProcess
guiProcess.botProcess = botProcess

botProcess = botProcess()
guiProcess = guiProcess()
botProcess.guiProcess = guiProcess
guiProcess.botProcess = botProcess

botProcess.start()
guiProcess.start()

BotProcess file:

class botProcess(multiprocessing.Process):

    guiProcess = ""

    def run(self):
        # Run Bot

    def someOtherMethod(self):
        # doSomething

GuiProcess file:

class guiProcess(multiprocessing.Process):

    botProcess = ""

    def run(self):
        # Display GUI
        botProcess.someOtherMethod();

Everytime i try to run a method on another process i get this error message:

AttributeError: 'NoneType' object has no attribute 'someOtherMethod'

Is there an easy way to fix this? Or should i just try something else (for example Threading) instead of Multiprocessing?

Sir_Arag0n
  • 13
  • 2
  • 2
    Why do you want to use processes instead of threads? – Blender May 24 '17 at 20:26
  • I agree with @Blender. Processes are best when you are doing many calculations, and threading for sharing information between threads and handling I/O. [Here](https://stackoverflow.com/questions/3044580/multiprocessing-vs-threading-python) is a pretty good explanation – SH7890 May 24 '17 at 20:31

1 Answers1

0

I left a comment on why I think you should use the Threading module instead of Multiprocessing, but if you want to stick to multiprocessing here is what I think your problem is: First of all, you should switch up the variable names so that they're not the same as your class.

bot_process = botProcess()

Using good variable names not only helps other people read your code, but also avoids any unexpected run time and syntax errors. Second, doing this:

botProcess = botProcess()
guiProcess = guiProcess()
botProcess.guiProcess = guiProcess
guiProcess.botProcess = botProcess

botProcess = botProcess()
guiProcess = guiProcess()
botProcess.guiProcess = guiProcess
guiProcess.botProcess = botProcess

Is redundant. You're just assigning the same variables to the same things twice.

Third, and probably what you actually want is that what you have in your classes are not members which is what you probably intended. Here is what you should have:

class guiProcess(multiprocessing.Process):

    def __init__(self):
        self.other_bot_process = None

    def run(self):
        # Display GUI
        self.other_bot_process.someOtherMethod();

(likewise in the other class) Then in your main file:

bot_process = botProcess()
gui_process = guiProcess()
bot_process.other_gui_process = gui_process
gui_process.other_bot_process = bot_process

Now, other_gui_process and other_bot_process are members of the class and instances of them can be accessed. Before they were just local variables which were only usable inside of the class where they were defined.

SH7890
  • 545
  • 2
  • 7
  • 20
  • Thanks a lot! I know my code is very sloppy, but i didnt have the testing code i wrote on hand, so i just threw together everything i still had in my mind. But anyway thanks a lot for your reply, and i think ill switch over to threading. – Sir_Arag0n May 25 '17 at 14:02