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为下载任务设置线程限制_Python_Multithreading - Fatal编程技术网

Python为下载任务设置线程限制

Python为下载任务设置线程限制,python,multithreading,Python,Multithreading,我是新手,已经在尝试用python创建文件下载管理器,但我有一个小问题。 我想将我的下载线程限制设置为=5,并希望强制管理器从单独的文件threads\u config.txt中获取此设置 在下面的代码示例中,我完全卡住了,不知道如何更改它: def downloadOnAThread(url): # It is ensured that the 'url' obtained here is downloadable # This function creates a threa

我是新手,已经在尝试用python创建文件下载管理器,但我有一个小问题。 我想将我的下载线程限制设置为=5,并希望强制管理器从单独的文件threads\u config.txt中获取此设置

在下面的代码示例中,我完全卡住了,不知道如何更改它:

def downloadOnAThread(url):
    # It is ensured that the 'url' obtained here is downloadable
    # This function creates a thread, pushes the coressponding filename and the progress onto the GUI and manages it
    # When the download finishes, the corresponding row is removed (opt. and all others are moved up?)
    u = urllib.request.urlopen(url)                                     # Open the File
    fileNameStr = url.split('/')[-1]
    shortenedFileName = shortenFileName(fileNameStr)
    shortenedFileNametk = tkinter.StringVar(window,value=shortenedFileName)     # Get the file name to write to
    downloadPath = os.path.join(os.path.expanduser(downloadLocation),fileNameStr)
    if os.path.exists(downloadPath):
        fileNameRandStr = ''.join(random.choices(string.ascii_uppercase + string.digits, k=4)) + ' ' + fileNameStr
        # fileName = tkinter.StringVar(window,value=fileNameRandStr)        # Get the file name to write to
        downloadPath = os.path.join(os.path.expanduser(downloadLocation),fileNameRandStr)
    totalFileSize = int(u.getheader("Content-Length"))                  # Get the total file size
    
    # print(totalFileSize, url)
    
    # Set up the GUI
    
    downloadingFileName = tkinter.Label(window, textvariable=shortenedFileNametk)
    downloadingFileName.grid(row=fileGUIRowNumber, column=0, columnspan=1, sticky=tkinter.E+tkinter.W)
    
    contentTypeLabel = parseFileType(u.getheader('Content-type'), window, fileGUIRowNumber)
    

    threadProgressBar = tkinter.ttk.Progressbar(window, orient=tkinter.HORIZONTAL, mode='determinate')
    threadProgressBar.grid(row=fileGUIRowNumber, column=2, columnspan=2, sticky=tkinter.E+tkinter.W)
    threadProgressBar['value']=0
    threadProgressBar['maximum']=totalFileSize

    cancelButton = tkinter.Button(window, text="Cancel", command= lambda : terminateThread(threading.current_thread(), f, contentTypeLabel, downloadingFileName, threadProgressBar, cancelButton, downloadPath, True))
    cancelButton.grid(row=fileGUIRowNumber, column=4, columnspan=1, sticky=tkinter.E+tkinter.W)
    # Begin downloading the Url
    f = open(downloadPath, 'wb')
    # else:
    #   terminateThread(threading.current_thread(), f, contentTypeLabel, downloadingFileName, threadProgressBar, cancelButton, downloadPath, False)
    downloadedFileSize = 0
    blockSize = 2**10 # 1024
    while True:
        buffer = u.read(blockSize)
        if not buffer:
            break
        downloadedFileSize += len(buffer)
        try:
            f.write(buffer)
        except ValueError:
            break
        threadProgressBar['value'] = downloadedFileSize
    terminateThread(threading.current_thread(), f, contentTypeLabel, downloadingFileName, threadProgressBar, cancelButton, downloadPath, False)
    # f.close()
    # downloadingFileName.grid_forget()
    # contentTypeLabel.grid_forget()
    # threadProgressBar.grid_forget()
    # cancelButton.grid_forget()
    # print("Thread Still executing")
    # print("url finished downloading")

def createThread():
    global threadJobList
    global fileGUIRowNumber
    ## !!! Caution !!! Using because the url is already verified
    url = textBoxContents.get()
    textBoxContents.set('')
    t = threading.Thread(target=downloadOnAThread, args=(url,))
    fileGUIRowNumber+=1
    threadJobList.append(t)
    t.daemon = False
    t.start()
    t.join(1)
    pass
你有什么想法吗


谢谢,

多处理模块有一个限制子流程的。在线程中也有相同的方法。