Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.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:启动类方法时线程不起作用 导入urllib.request 导入URL库 进口舒蒂尔 导入操作系统 导入操作系统路径 导入系统 导入时间 导入线程 类下载文件: 定义初始化(self、downloadLink、downloadPath、onDiskName): self.downloadSize=urllib.request.urlopen(downloadLink).length self.downloadLink=下载链接 self.downloadPath=下载路径 self.onDiskName=onDiskName self.hardPath=os.path.join(self.downloadPath,self.onDiskName) def returnMode(self,returnMode='stats'): 如果returnMode=='stats': return[self.downloadLink,self.downloadPath,self.onDiskName,self.downloadSize] elif returnMode==“printedStats”: 打印('self.downloadLink='+self.downloadLink) 打印('self.downloadPath='+self.downloadPath) 打印('self.onDiskName='+self.onDiskName) 打印('self.downloadSize='+self.downloadSize) 打印('self.hardPath='+self.hardPath) return[self.downloadLink、self.downloadPath、self.onDiskName、self.downloadSize、self.hardPath] def executeDownload(自): self.downloadStart=time.strftime(“[%H:%M:%S]”) 使用urllib.request.urlopen(self.downloadLink)作为响应,将open(self.hardPath,'wb')作为当前文件: copyfileobj(响应,当前文件) currentFile.close() self.downloadEnd=time.strftime(“[%H:%M:%S]”) def下载状态(自我): currentFileSize=os.path.getsize(self.hardPath) 百分比=int(currentFileSize/(self.downloadSize/100)) 返回[当前文件大小,百分比] def liveDownloadStats(自): 如果os.path.isfile(self.hardPath)=False: 时间。睡眠(1) statList=self.downloadStats() 而statList[0]_Python_Multithreading_Class_Methods - Fatal编程技术网

Python:启动类方法时线程不起作用 导入urllib.request 导入URL库 进口舒蒂尔 导入操作系统 导入操作系统路径 导入系统 导入时间 导入线程 类下载文件: 定义初始化(self、downloadLink、downloadPath、onDiskName): self.downloadSize=urllib.request.urlopen(downloadLink).length self.downloadLink=下载链接 self.downloadPath=下载路径 self.onDiskName=onDiskName self.hardPath=os.path.join(self.downloadPath,self.onDiskName) def returnMode(self,returnMode='stats'): 如果returnMode=='stats': return[self.downloadLink,self.downloadPath,self.onDiskName,self.downloadSize] elif returnMode==“printedStats”: 打印('self.downloadLink='+self.downloadLink) 打印('self.downloadPath='+self.downloadPath) 打印('self.onDiskName='+self.onDiskName) 打印('self.downloadSize='+self.downloadSize) 打印('self.hardPath='+self.hardPath) return[self.downloadLink、self.downloadPath、self.onDiskName、self.downloadSize、self.hardPath] def executeDownload(自): self.downloadStart=time.strftime(“[%H:%M:%S]”) 使用urllib.request.urlopen(self.downloadLink)作为响应,将open(self.hardPath,'wb')作为当前文件: copyfileobj(响应,当前文件) currentFile.close() self.downloadEnd=time.strftime(“[%H:%M:%S]”) def下载状态(自我): currentFileSize=os.path.getsize(self.hardPath) 百分比=int(currentFileSize/(self.downloadSize/100)) 返回[当前文件大小,百分比] def liveDownloadStats(自): 如果os.path.isfile(self.hardPath)=False: 时间。睡眠(1) statList=self.downloadStats() 而statList[0]

Python:启动类方法时线程不起作用 导入urllib.request 导入URL库 进口舒蒂尔 导入操作系统 导入操作系统路径 导入系统 导入时间 导入线程 类下载文件: 定义初始化(self、downloadLink、downloadPath、onDiskName): self.downloadSize=urllib.request.urlopen(downloadLink).length self.downloadLink=下载链接 self.downloadPath=下载路径 self.onDiskName=onDiskName self.hardPath=os.path.join(self.downloadPath,self.onDiskName) def returnMode(self,returnMode='stats'): 如果returnMode=='stats': return[self.downloadLink,self.downloadPath,self.onDiskName,self.downloadSize] elif returnMode==“printedStats”: 打印('self.downloadLink='+self.downloadLink) 打印('self.downloadPath='+self.downloadPath) 打印('self.onDiskName='+self.onDiskName) 打印('self.downloadSize='+self.downloadSize) 打印('self.hardPath='+self.hardPath) return[self.downloadLink、self.downloadPath、self.onDiskName、self.downloadSize、self.hardPath] def executeDownload(自): self.downloadStart=time.strftime(“[%H:%M:%S]”) 使用urllib.request.urlopen(self.downloadLink)作为响应,将open(self.hardPath,'wb')作为当前文件: copyfileobj(响应,当前文件) currentFile.close() self.downloadEnd=time.strftime(“[%H:%M:%S]”) def下载状态(自我): currentFileSize=os.path.getsize(self.hardPath) 百分比=int(currentFileSize/(self.downloadSize/100)) 返回[当前文件大小,百分比] def liveDownloadStats(自): 如果os.path.isfile(self.hardPath)=False: 时间。睡眠(1) statList=self.downloadStats() 而statList[0],python,multithreading,class,methods,Python,Multithreading,Class,Methods,一旦文件出现,就应该开始打印下载的文件的下载百分比。我看到的是,文件出现了,片刻之后,我得到了输出,说它是100%下载的。我看不出我到底做错了什么。问题是,每次它检查当前下载的数据和需要下载的数据时,它都没有更新变量。因此,当循环出现时,它会比较相同的数字,直到出现某种情况。它被困在这个循环中,当有什么东西导致它退出时,我不知道是什么,它继续到下一行代码打印,它已经下载完毕 import urllib.request import urllib import shutil import os i

一旦文件出现,就应该开始打印下载的文件的下载百分比。我看到的是,文件出现了,片刻之后,我得到了输出,说它是100%下载的。我看不出我到底做错了什么。

问题是,每次它检查当前下载的数据和需要下载的数据时,它都没有更新变量。因此,当循环出现时,它会比较相同的数字,直到出现某种情况。它被困在这个循环中,当有什么东西导致它退出时,我不知道是什么,它继续到下一行代码打印,它已经下载完毕

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)
导入urllib.request
导入URL库
进口舒蒂尔
导入操作系统
导入操作系统路径
导入系统
导入时间
导入线程
类下载文件:
定义初始化(self、downloadLink、downloadPath、onDiskName):
self.downloadSize=urllib.request.urlopen(downloadLink).length
self.downloadLink=下载链接
self.downloadPath=下载路径
self.onDiskName=onDiskName
self.hardPath=os.path.join(self.downloadPath,self.onDiskName)
def returnMode(self,returnMode='stats'):
如果returnMode=='stats':
return[self.downloadLink,self.downloadPath,self.onDiskName,self.downloadSize]
elif returnMode==“printedStats”:
打印('self.downloadLink='+self.downloadLink)
打印('self.downloadPath='+self.downloadPath)
打印('self.onDiskName='+self.onDiskName)
打印('self.downloadSize='+self.downloadSize)
打印('self.hardPath='+self.hardPath)
return[self.downloadLink、self.downloadPath、self.onDiskName、self.downloadSize、self.hardPath]
def executeDownload(自):
self.downloadStart=time.strftime(“[%H:%M:%S]”)
使用urllib.request.urlopen(self.downloadLink)作为响应,将open(self.hardPath,'wb')作为当前文件:
copyfileobj(响应,当前文件)
currentFile.close()
self.downloadEnd=time.strftime(“[%H:%M:%S]”)
def下载状态(自我):
currentFileSize=os.path.getsize(self.hardPath)
百分比=int(currentFileSize/(self.downloadSize/100))
返回[当前文件大小,百分比]
def liveDownloadStats(自):
如果os.path.isfile(self.hardPath)=False:
时间。睡眠(1)
statList=self.downloadStats()
而statList[0]
可能下载得太快了?我已经测试过了,
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()