使用Tkinter(Python 2.7)连续运行长进程 [英] Run long process continously using Tkinter (Python 2.7)

查看:254
本文介绍了使用Tkinter(Python 2.7)连续运行长进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间后试图弄清楚Tkinter库的工作方式,我遇到了一个问题.我编写的脚本使用了多重处理,并且由于脚本需要尽可能快,因此我将进程之间的通信量减至最少.这意味着完成大量任务大约需要一分钟.

After some time trying to figure out how the Tkinter library works I have run into a problem. The script i wrote uses multiprocessing, and because the script needs to be as fast as possible I minimized the amount of traffic between the processes. This means that it takes about a minute to complete an enormous amount of tasks.

(如果该任务在中途中止,则使用的文件将被破坏).

(If this task gets aborted halfway through, the used files will get corrupted).

问题是我希望GUI中的停止按钮以正确的方式停止脚本.经过一番研究之后,我在寻找解决方案方面没有取得任何进展,因此也许有些人可以提供帮助.我基本上需要一种方法在执行任务的一半时告诉脚本它必须停止,此后脚本将继续执行直到任务完成.

The problem is that i want a stop button in my GUI to stop the script the proper way. After some research i haven't made any progress in finding a solution, so maybe some of you could help. I basically need a way to tell the script halfway through a task that it has to stop, after which the script will continue until the task is finished.

我的脚本的设置方式:

The way my script is set up:

(这缺少Tkinter部分,因为我还不知道解决方案).

(This is missing the Tkinter part, because i don't know the solution to it yet).

from multiprocessing import Pool

def Setup():
    #defines all paths of the files that are edited (and a whole lot more)

def Calculation(x, y, Primes):
    #takes an x and y value, calculates the value of that coordinate and determines
    #if the value is prime. Returns True of False, and the calculated value.

def Quadrant(List):
    #takes a huge list of coordinates that have to be calculated. These
    #coordinates (x and y) are passed to the 'Calculation' function, one by one.

    #Returns all the calculated coordinates and if they are prime or not (boolean)

if __name__ == "__main__":
    Filenames = Setup()
    Process = Pool(4)

    while True:
        #Loop the main bit of the code to keep expanding the generated image
        Input = [List of all coordinates, split into 4 quadrants (seperate lists) evenly]
        Output = Process.map(Quadrant, Input)

        #Combine all data and update list of primes

        #Detects if escape is pressed, stops if true.

我基本上是在寻找一种方法来停止上面的while循环或该循环的替代方法.

I am basically looking for a way to stop the while loop above, or an alternative to this loop.

推荐答案

我基本上是指该任务必须停止,而不能突然终止.该脚本必须等到其任务完成后,然后查看是否按下了按钮来决定是否必须继续

我们没有代码可响应,因此,如果您使用while()(请注意,如果某些条件为True/False,您也可以从函数中返回结果).

We have no code from you to respond to, so if you are using a while() (note that you can also just issue a return from the function if some condition is True/False).

import time
from multiprocessing import Process, Manager

def test_f(test_d):
   """  frist process to run
        exit this process when dictionary's 'QUIT' == True
   """
   while not test_d["QUIT"]:
      print "     test_f", test_d["QUIT"]
      time.sleep(1.0)

def test_f2(name):
    """ second process to run.  Runs until the for loop exits
   """
    for j in range(0, 10):
       print name, j
       time.sleep(0.5)

    print "second process finished"

if __name__ == '__main__':
   ##--- create a dictionary via Manager
   manager = Manager()
   test_d = manager.dict()
   test_d["QUIT"] = False

   ##---  start first process and send dictionary
   p = Process(target=test_f, args=(test_d,))
   p.start()

   ##--- start second process
   p2 = Process(target=test_f2, args=('P2',))
   p2.start()

   ##--- sleep 2 seconds and then change dictionary
   ##     to exit first process
   time.sleep(2.0)
   print "\nterminate first process"
   test_d["QUIT"] = True
   print "test_d changed"
   print "data from first process", test_d

   ##---  may not be necessary, but I always terminate to be sure
   time.sleep(5.0)
   p.terminate()
   p2.terminate()

   """ Thanks Doug Hellmann
       Note: It is important to join() the process after terminating it.
       in order to give the background machinery time to update the.
       status of the object to reflect the termination
   """
   p.join()
   p2.join()

这篇关于使用Tkinter(Python 2.7)连续运行长进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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