I was following this guide to try and see if I could get a simple multiprocessing script to run. It's been discussed in other questions that I need to use multiprocessing for what I'm trying to achieve.
For the sake of following the beginner guide, I wrote two functions.
import pyautogui as py
import multiprocessing
def print_i():
for i in range(0, 21):
print(i)
sleep(0.1)
image = r'C:\Users\image.jpg'
def closeWindow():
while True:
found = py.locateCenterOnScreen(image)
if found != None:
py.click(1797, 134)
print_i
prints out the numbers 0-20 and closeWindow
checks if a particular window is open then clicks to close it if it finds it, both work individually.
In following the guide I've imported multiprocessing
and done the following:
p1 = multiprocessing.Process(target= print_i)
p2 = multiprocessing.Process(target= closeWindow)
p1.start()
p2.start()
p1.join()
p2.join()
However, I immediately get following error twice:
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
This stack explains that the multiprocessing module should take care of these cases and detect that you are trying to start new processes even before the target was run.
In reading the error in mentions not using fork
to start a child process, I didn't think I had a child process, does anything after your first process count as a child process when multiprocessing? If yes, how can I output the numbers 0-20 with one process while running the image detection at the same time?