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

2024-09-30 01:27:18 发布

您现在位置:Python中文网/ 问答频道 /正文

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%下载。我看不出我到底做错了什么。你知道吗


Tags: pathimportselftimeosdefsysurllib
1条回答
网友
1楼 · 发布于 2024-09-30 01:27:18

问题是每次它检查当前下载的数据和需要下载的数据时,都没有更新变量。所以当循环出现时,它会比较相同的数字,直到有结果。它被卡在这个循环中,当有什么东西导致它退出时,我不知道是什么,它继续到下载完成的下一行代码打印。你知道吗

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()

相关问题 更多 >

    热门问题