Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 帮助为GUI进度添加线程_Python_Multithreading_Ftp - Fatal编程技术网

Python 帮助为GUI进度添加线程

Python 帮助为GUI进度添加线程,python,multithreading,ftp,Python,Multithreading,Ftp,我有一个FTP功能,可以跟踪运行上传的进度,但我对线程的理解有限,我无法实现一个有效的解决方案。。。我想通过使用线程将GUI进度条添加到当前应用程序中。有人能给我演示一个使用异步线程的基本函数吗?异步线程可以从另一个正在运行的线程更新 def ftpUploader(): BLOCKSIZE = 57344 # size 56 kB ftp = ftplib.FTP() ftp.connect(host) ftp.login(login, passwd)

我有一个FTP功能,可以跟踪运行上传的进度,但我对线程的理解有限,我无法实现一个有效的解决方案。。。我想通过使用线程将GUI进度条添加到当前应用程序中。有人能给我演示一个使用异步线程的基本函数吗?异步线程可以从另一个正在运行的线程更新

def ftpUploader():
    BLOCKSIZE = 57344 # size 56 kB

    ftp = ftplib.FTP()
    ftp.connect(host)
    ftp.login(login, passwd)
    ftp.voidcmd("TYPE I")
    f = open(zipname, 'rb')
    datasock, esize = ftp.ntransfercmd(
        'STOR %s' % os.path.basename(zipname))
    size = os.stat(zipname)[6]
    bytes_so_far = 0
    print 'started'
    while 1:
        buf = f.read(BLOCKSIZE)
        if not buf:
            break
        datasock.sendall(buf)
        bytes_so_far += len(buf)
        print "\rSent %d of %d bytes %.1f%%\r" % (
              bytes_so_far, size, 100 * bytes_so_far / size)
        sys.stdout.flush()

    datasock.close()
    f.close()
    ftp.voidresp()
    ftp.quit()
    print 'Complete...'

这里是线程的一个快速概述,以防万一:)我不会对GUI的内容进行太多的详细介绍,只是说您应该查看wxWidgets。每当你做一些需要很长时间的事情时,比如:

from time import sleep
for i in range(5):
    sleep(10)
您会注意到,对于用户来说,整个代码块似乎需要50秒。在这5秒钟内,您的应用程序无法执行任何类似于更新接口的操作,因此它看起来像是被冻结了。为了解决这个问题,我们使用线程

这个问题通常有两个部分;你想要处理的所有事情,以及我们想要分割的需要一段时间的操作。在本例中,整个集合是for循环,我们想要切碎的操作是sleep(10)函数

下面是一个基于前面示例的线程代码快速模板。您应该能够在这个示例中使用您的代码

from threading import Thread
from time import sleep

# Threading.
# The amount of seconds to wait before checking for an unpause condition.
# Sleeping is necessary because if we don't, we'll block the os and make the
# program look like it's frozen.
PAUSE_SLEEP = 5

# The number of iterations we want.
TOTAL_ITERATIONS = 5

class myThread(Thread):
    '''
    A thread used to do some stuff.
    '''
    def __init__(self, gui, otherStuff):
        '''
        Constructor. We pass in a reference to the GUI object we want
        to update here, as well as any other variables we want this
        thread to be aware of.
        '''
        # Construct the parent instance.
        Thread.__init__(self)

        # Store the gui, so that we can update it later.
        self.gui = gui

        # Store any other variables we want this thread to have access to.
        self.myStuff = otherStuff

        # Tracks the paused and stopped states of the thread.
        self.isPaused = False
        self.isStopped = False

    def pause(self):
        '''
        Called to pause the thread.
        '''
        self.isPaused = True

    def unpause(self):
        '''
        Called to unpause the thread.
        '''
        self.isPaused = False

    def stop(self):
        '''
        Called to stop the thread.
        '''
        self.isStopped = True

    def run(self):
        '''
        The main thread code.
        '''
        # The current iteration.
        currentIteration = 0

        # Keep going if the job is active.
        while self.isStopped == False:
            try:
                # Check for a pause.
                if self.isPaused:
                    # Sleep to let the os schedule other tasks.
                    sleep(PAUSE_SLEEP)
                    # Continue with the loop.
                    continue

                # Check to see if we're still processing the set of
                # things we want to do.
                if currentIteration < TOTAL_ITERATIONS:
                    # Do the individual thing we want to do.
                    sleep(10)
                    # Update the count.
                    currentIteration += 1
                    # Update the gui.
                    self.gui.update(currentIteration,TOTAL_ITERATIONS)
                else:
                    # Stop the loop.
                    self.isStopped = True

            except Exception as exception:
                # If anything bad happens, report the error. It won't
                # get written to stderr.
                print exception
                # Stop the loop.
                self.isStopped = True

        # Tell the gui we're done.
        self.gui.stop()

不要使用新线程中的GUI。为单个线程保留所有GUI操作,并让第二个线程执行FTP。我可以使用提供的示例插入并运行代码,但我仍然不确定如何更新主窗口。。。如何将状态传递回原始GUI实例?顺便说一句:我使用wxPython..当你构造你的线程时,把窗口作为一个参数传递给线程,并在本地存储窗口。那是self.gui。完成后,可以在主线程上调用一个方法来更新它。
aThread = myThread(myGui,myOtherStuff)
aThread.start()