Python:启动类方法时,线程不起作用

时间:2014-11-20 04:42:19

标签: python multithreading class methods

import urllib.request
import urllib
import shutil
import os
import os.path
import sys
import time
import threading

class downloadFile:

    def __init__(self, downloadLink, downloadPath, onDiskName):
        self.downloadSize = urllib.request.urlopen(downloadLink).length
        self.downloadLink = downloadLink
        self.downloadPath = downloadPath
        self.onDiskName = onDiskName
        self.hardPath = os.path.join(self.downloadPath, self.onDiskName)

    def returnMode(self, returnMode = 'stats'):
        if returnMode == 'stats':
            return [self.downloadLink, self.downloadPath, self.onDiskName, self.downloadSize]
        elif returnMode == 'printedStats':
            print('self.downloadLink = ' + self.downloadLink)
            print('self.downloadPath = ' + self.downloadPath)
            print('self.onDiskName = ' + self.onDiskName)
            print('self.downloadSize = ' + self.downloadSize)
            print('self.hardPath = ' + self.hardPath)
            return [self.downloadLink, self.downloadPath, self.onDiskName, self.downloadSize, self.hardPath]

    def executeDownload(self):
        self.downloadStart = time.strftime("[%H:%M:%S] ")
        with urllib.request.urlopen(self.downloadLink) as response, open(self.hardPath, 'wb') as currentFile:
            shutil.copyfileobj(response, currentFile)
        currentFile.close()
        self.downloadEnd = time.strftime("[%H:%M:%S] ")

    def downloadStats(self):
        currentFileSize = os.path.getsize(self.hardPath)
        percentManifested = int(currentFileSize/(self.downloadSize/100))
        return [currentFileSize, percentManifested]

    def liveDownloadStats(self):
        if os.path.isfile(self.hardPath) == False:
            time.sleep(1)
        statList = self.downloadStats()
        while statList[0] < self.downloadSize:
            sys.stdout.write("\r" + self.downloadStart + " Downloading {0}... ".format(self.onDiskName) + "[{0}%]".format(statList[1]))
            sys.stdout.flush()
        sys.stdout.write("\r" + self.downloadStart + " Downloading {0}... ".format(self.onDiskName) + "[{0}%]".format(statList[1]))
        sys.stdout.write("\n")


server = downloadFile("https://s3.amazonaws.com/Minecraft.Download/versions/1.8/minecraft_server.1.8.jar", "C:/Users/my-username/Desktop/", "minecraftServer.jar")
t1 = threading.Thread(target=server.executeDownload())
t2 = threading.Thread(target=server.liveDownloadStats())
t2.start()
t1.start()
time.sleep(100)

这应该是在文件出现后开始打印正在下载的文件的下载百分比。我所看到的是该文件出现,然后不久我得到输出说它是100%下载。我无法确切地看到我在这里做错了什么。

1 个答案:

答案 0 :(得分:1)

问题在于,每次检查当前下载的数据到需要下载的数据时,它都不会更新变量。因此,当循环出现时,它正在比较相同的数字直到某些东西。它停留在这个循环中,当某些东西导致它退出时,我不确定是什么,它继续下一行代码打印,它已完成下载。

import urllib.request
import urllib
import shutil
import os
import os.path
import sys
import time
import threading

class downloadFile:

    def __init__(self, downloadLink, downloadPath, onDiskName):
        self.downloadSize = urllib.request.urlopen(downloadLink).length
        self.downloadLink = downloadLink
        self.downloadPath = downloadPath
        self.onDiskName = onDiskName
        self.hardPath = os.path.join(self.downloadPath, self.onDiskName)

    def returnMode(self, returnMode = 'stats'):
        if returnMode == 'stats':
            return [self.downloadLink, self.downloadPath, self.onDiskName, self.downloadSize]
        elif returnMode == 'printedStats':
            print('self.downloadLink = ' + self.downloadLink)
            print('self.downloadPath = ' + self.downloadPath)
            print('self.onDiskName = ' + self.onDiskName)
            print('self.downloadSize = ' + self.downloadSize)
            print('self.hardPath = ' + self.hardPath)
            return [self.downloadLink, self.downloadPath, self.onDiskName, self.downloadSize, self.hardPath]

    def executeDownload(self):
        self.downloadStart = time.strftime("[%H:%M:%S] ")
        with urllib.request.urlopen(self.downloadLink) as response, open(self.hardPath, 'wb') as currentFile:
            shutil.copyfileobj(response, currentFile)
        currentFile.close()
        self.downloadEnd = time.strftime("[%H:%M:%S] ")

    def downloadStats(self):
        currentFileSize = os.path.getsize(self.hardPath)
        percentManifested = int(currentFileSize/(self.downloadSize/100))
        return [currentFileSize, percentManifested]

    def liveDownloadStats(self):
        if os.path.isfile(self.hardPath) == False:
            time.sleep(1)
        statList = self.downloadStats()
        while statList[0] < self.downloadSize:
            sys.stdout.write("\r" + self.downloadStart + " Downloading {0}... ".format(self.onDiskName) + "[{0}%]".format(statList[1]))
            sys.stdout.flush()
            statList = self.downloadStats() #This is the extra line of code used to update the variable before comparing on the next while loop.
        sys.stdout.write("\r" + self.downloadStart + " Downloading {0}... ".format(self.onDiskName) + "[{0}%]".format(statList[1]))
        sys.stdout.write("\n")

def Main():
    server = downloadFile("https://s3.amazonaws.com/Minecraft.Download/versions/1.8/minecraft_server.1.8.jar", "C:/Users/my-username/Desktop/", "minecraftServer.jar")
    t1 = threading.Thread(target=server.executeDownload, args=())
    t2 = threading.Thread(target=server.liveDownloadStats, args=())
    t1.start()
    t2.start()

if __name__ == "__main__":
    Main()