Concurrent.futures 在 tkinter 中打开新窗口而不是运行该函数 [英] Concurrent.futures opens new windows in tkinter instead of running the function

查看:32
本文介绍了Concurrent.futures 在 tkinter 中打开新窗口而不是运行该函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 tkinter 和 concurrent.futures 在 gui 中的多个文件上同时运行一个函数

I am trying to run a function concurrently over multiple files in a gui using tkinter and concurrent.futures

在 GUI 之外,此脚本运行良好.但是,每当我将其转换为 GUI 脚本时,该脚本不会并行运行该函数,而是会打开 5 个新的 gui tkinter 窗口(它打开的窗口数等于我允许程序使用的处理器数).

Outside of the GUI, this script works fine. However, whenever I translate it over into the GUI script, instead of running the function in parallel, the script opens up 5 new gui tkinter windows (the number of windows it opens is equal to the number of processors I allow the program to use).

我已经彻底查看了代码,只是无法理解为什么它会打开新窗口,而不是仅仅在文件上运行该函数.

Ive looked over the code thoroughly and just cant understand why it is opening new windows as opposed to just running the function over the files.

有人能看到我遗漏的东西吗?

Can anyone see something I am missing?

代码的删减版本如下.我已经删除了代码的重要部分,只留下了与并行化相关的部分.这段代码无疑包含我在本示例中未定义的变量.

An abridged version of the code is below. I have cut out a significant part of the code and only left in the parts pertinent to parralelization. This code undoubtedly has variables in it that I have not defined in this example.

import pandas as pd 
import numpy as np
import glob
from pathlib import Path
from tkinter import *
from tkinter import filedialog
from concurrent.futures import ProcessPoolExecutor

window = Tk()
window.title('Problem with parralelizing')
window.geometry('1000x700')


def calculate():
    #establish where the files are coming from to operate on
    folder_input = folder_entry_var.get()
    #establish the number of processors to use
    nbproc = int(np_var.get())

    #loop over files to get a list of file to be worked on by concurrent.futures
    files = []
    for file in glob.glob(rf'{folder_input}'+'//*'):
        files.append(file)

    #this function gets passed to concurrent.futures. I have taken out a significant portion of 
    #function itself as I do not believe the problem resides in the function itself.
    def process_file(filepath):

        excel_input = excel_entry_var.get()
        minxv = float(min_x_var.get())
        maxxv = float(man_x_var.get())
        output_dir = odir_var.get()


        path = filepath

        event_name = Path(path).stem
        event['event_name'] = event_name




        min_x = 292400
        max_x = 477400

        list_of_objects = list(event.object.unique())

        missing_master_surface = []

        for line in list_of_objects:


            df = event.loc[event.object == line]

            current_y = df.y.max()
            y_cl = df.x.values.tolist()
            full_ys = np.arange(min_x,max_x+200,200).tolist()

            for i in full_ys:
                missing_values = []
                missing_v_y = []
                exist_yn = []
                event_name_list = []

                if i in y_cl:
                    next
                elif i not in y_cl:
                    missing_values.append(i)
                    missing_v_y.append(current_y)
                    exist_yn.append(0)
                    event_name_list.append(event_name)


    # feed the function to processpool executer to run. At this point, I hear the processors
    # spin up, but all it does is open 5 new tkinter windows (the number of windows is proportionate
    #to the number of processors I give it to run
    if __name__ == '__main__': 
        with ProcessPoolExecutor(max_workers=nbproc) as executor:
            executor.map(process_file, files)

window.mainloop()

推荐答案

我已经彻底查看了代码,只是无法理解为什么它会打开新窗口,而不是仅仅在文件上运行该函数.

Ive looked over the code thoroughly and just cant understand why it is opening new windows as opposed to just running the function over the files.

每个进程都必须重新加载您的代码.在代码的最顶部,您执行 window = Tk().这就是每个进程获得一个窗口的原因.

Each process has to reload your code. At the very top of your code you do window = Tk(). That is why you get one window per process.

这篇关于Concurrent.futures 在 tkinter 中打开新窗口而不是运行该函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆