Python中的多线程与并行处理
好的,当我第一次尝试python编程(我用其他语言编写代码,只是从来没有用python)时,我决定制作一个“机器人”来玩流行的游戏Friday Night Funkin。我相信除了跟踪每个箭头的部分之外,我的代码的其他部分都可以工作 我正在尝试运行一个通用方法,我编写该方法来解释4个箭头中的一个Python中的多线程与并行处理,python,multithreading,multiprocessing,Python,Multithreading,Multiprocessing,好的,当我第一次尝试python编程(我用其他语言编写代码,只是从来没有用python)时,我决定制作一个“机器人”来玩流行的游戏Friday Night Funkin。我相信除了跟踪每个箭头的部分之外,我的代码的其他部分都可以工作 我正在尝试运行一个通用方法,我编写该方法来解释4个箭头中的一个 def generic_arrow_logic(arrType, thread): global color_dictionary, key_dictionary print("
def generic_arrow_logic(arrType, thread):
global color_dictionary, key_dictionary
print("Thread {0} running...".format(thread))
while (True):
wholeImage = screenGrab("curr")
currArr = cutImage(wholeImage, arrType, "not")
currArr.save(arrowArrayCurr)
currArr_rgb = currArr.convert("RGB")
if(currArr_rgb.getpixel((currArr.width/2, currArr.height/2)) == color_dictionary[arrType]):
# pyautogui.press(key_dictionary[arrType])
keyboard.write(key_dictionary[arrType],delay=0)
print("Thread:{0} - {1}, Detected...".format(str(thread), str(arrType).upper()))
我在底部附上了打印报表,以检查其是否有效
到目前为止,我的两次尝试都使用了多处理模块和线程模块,如下所示:
pool = Pool(processes = 4)
for dir in direction_array:
pool.apply_async(generic_arrow_logic, args=(dir,direction_array.index(dir)))
print("starting thread " + str(direction_array.index(dir)))
pool.close()
pool.join()
和线程:
for dir in direction_array:
threading.Thread(target=generic_arrow_logic(dir, direction_array.index(dir))).start()
这里有颜色字典、方向数组和键字典供参考:
color_dictionary = {"right":right, "left":left_arrow_color, "up":up_arrow_color, "down":down_arrow_color, "default":default_arrow_color}
direction_array = ["right", "left", "up", "down"]
key_dictionary = {"right":rightKey, "left":leftKey, "up":upKey, "down":downKey}
编辑:我意识到人们并不真正知道我在问什么。我的问题是,“我应该如何同时运行任何给定的方法?”。目前,当我运行我的程序时,我会得到两种不同的输出中的一种。
当我运行多处理程序时,它无法生成进程,并最终循环程序的一个完全不同的部分,如下所示
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\Jalen Morgan\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 116, in spawn_main
exitcode = _main(fd, parent_sentinel)
File "C:\Users\Jalen Morgan\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 125, in _main
prepare(preparation_data)
File "C:\Users\Jalen Morgan\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 236, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "C:\Users\Jalen Morgan\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from_path
main_content = runpy.run_path(main_path,
File "C:\Users\Jalen Morgan\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 268,
in run_path
问题是什么?编辑以反映实际问题
---------Welcome to my FNF Bot--------
-----------Select an Option-----------
Region: ((1758, 316),(1459, 244))
1.) Select region
2.) Start Bot
3.) Exit
Enter an option: 2
Thread 0 running...