0

Good evening. I am working on a program that uses multiprocessing, tkinter and Selenium. I'm trying to pass my Selenium instance to a new process but it's giving me this giant error:

Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "c:\Users\user\Desktop\coding\PokeClicker Bot\Version 3 (Gui) (WIP)\version3.py", line 37, in <lambda>
    item = tk.Button(root, text='Run', relief='raised', command=lambda:toggle(item))
  File "c:\Users\user\Desktop\coding\PokeClicker Bot\Version 3 (Gui) (WIP)\version3.py", line 54, in toggle
    startRun()
  File "c:\Users\user\Desktop\coding\PokeClicker Bot\Version 3 (Gui) (WIP)\version3.py", line 62, in startRun
    p.start()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\multiprocessing\process.py", line 121, in start
    self._popen = self._Popen(self)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\multiprocessing\context.py", line 224, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\multiprocessing\context.py", line 327, in _Popen
    return Popen(process_obj)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\multiprocessing\popen_spawn_win32.py", line 93, in __init__
    reduction.dump(process_obj, to_child)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
TypeError: cannot pickle '_io.TextIOWrapper' object
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\multiprocessing\spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\multiprocessing\spawn.py", line 126, in _main
    self = reduction.pickle.load(from_parent)
EOFError: Ran out of input

Here's the code, trimmed down for readability:

def init():
    global ffD
    ffD = WD.Firefox(os.getcwd(), options=options)
    ffD.get('https://www.pokeclicker.com/')
    root.mainloop()
    
def runThroughDungeon(driver): #Currently just driver.close() to test.
    driver.close()

def startRun(): #Called from toggling tkinter button.
    global p
    p = mp.Process(target = runThroughDungeon, args=(ffD,))
    p.start()

def stopRun():
    p.kill()

if __name__ == '__main__':
    init()

I'm thinking this is an error due to Selenium having arguments that aren't transferred during the startRun() process? I'm not really sure, though.

Michael S.
  • 3,050
  • 4
  • 19
  • 34
cheis
  • 1
  • 4
  • I seriously doubt you'll be able to share the selenium instance between processes. You definitely can't use tkinter from more than one process. – Bryan Oakley Aug 30 '22 at 22:15
  • @BryanOakley If that's the case, should I consider change to a language that natively supports multiprocessing? – cheis Aug 30 '22 at 22:18
  • Not necessarily. You can use multiprocesses, there is just some sorts of data that can't be shared. If only the tkinter process accessed the GUI, and only the selenium processess accessed selenium, and you used IPC to coordinate, it can work. – Bryan Oakley Aug 30 '22 at 22:20
  • @BryanOakley I see. Would you know any IPC frameworks that could probably get this job done? – cheis Aug 30 '22 at 22:24
  • You can use queues to pass data around. Maybe shared memory. XMLRPC. – Bryan Oakley Aug 30 '22 at 22:26
  • @BryanOakley I'll look into shared memory, I think that will fit my application the best. Thank you. – cheis Aug 30 '22 at 22:27
  • @cheis you can use multiple separate instances of selenium each in their own process. I've done it several times. What you can't do is have multiple processes all controlling a single instance. – Aaron Aug 30 '22 at 23:26
  • every process has to run own Selenium. You can't send Selenium from one process to other process. – furas Aug 31 '22 at 00:43
  • See [this post](https://stackoverflow.com/questions/53475578/python-selenium-multiprocessing) and my answer that ensures that the drivers are properly terminated. – Booboo Sep 02 '22 at 11:24

0 Answers0