Python multiprocessing.pool()无限期挂起
我试图通过使用Python multiprocessing.pool()无限期挂起,python,python-3.x,multiprocessing,pool,Python,Python 3.x,Multiprocessing,Pool,我试图通过使用多处理.Pool,让我的程序更快地处理数据,但在实现它时遇到了一些问题 我的程序有一个主文件,其中运行着tkinter GUI,如下所示: WIDTH = 1345 HEIGHT = 665 root = tk.Tk() Calculate(Config, matrices): (Calls the function Manager(), which is stored in another .py) def main(): --variables, bind
多处理.Pool
,让我的程序更快地处理数据,但在实现它时遇到了一些问题
我的程序有一个主文件,其中运行着tkinter GUI,如下所示:
WIDTH = 1345
HEIGHT = 665
root = tk.Tk()
Calculate(Config, matrices):
(Calls the function Manager(), which is stored in another .py)
def main():
--variables, bindings and stuff--
btncalc.bind('<Button-1>', lambda event: Calculate(Config=Config,matrices=matrices))
if __name__ == '__main__':
main()
有趣的是,有时这段代码确实有效,有时它只是挂在
pool.map
上。我不知道为什么它有时会挂起来,但有时确实很好用。也许我应该使用进程
和队列
而不是池
?因为它只是无限期地挂起,所以没有错误消息,所以我不知道从哪里开始调试代码。我不知道这是否重要,但我正在使用Python3.8。如何将tkinter
融入这一切?这很可能是关键,因为它不支持多处理或多线程。然后把它连接到你的按钮上。但是您可以尝试一些事情:1)在主函数中创建池并将其作为参数传递;2)不要阻止tk的事件循环,将按钮调用分派给线程
def worker(lista):
a,b,c,d,e = lista
(processing)
return [f,g,h,i]
def MultiprocessingFunc(a,b,c,d,e):
lista = []
results = []
for p in range(len(a)):
lista.append([a[p],b,c,d,e])
pool = Pool(os.cpu_count()) #in my case this is 4
results.append(pool.map(worker, lista)) #Hangs here sometimes
pool.close()
pool.join() #Hangs here if imap
return results
def Manager(Config, matrices):
(prepares stuff)
results = MultiprocessingFunc(a,b,c,d,e)
(uses the results)