简体   繁体   English

缺少Python和多线程sys.excepthook

[英]Python and multithreading sys.excepthook is missing

So I have many files to download from a server. 所以我有很多文件要从服务器上下载。 Over 1000... So I thought I'd write a script that would do this for me that is multithreaded so that I don't have to wait for ages for it to finish. 超过1000 ......所以我想我会编写一个脚本来为我做这个多线程的脚本,这样我就不必等待很长时间才能完成。 The problem is that it spits out a bunch of errors. 问题是它吐出了一堆错误。 I have searched for this but couldn't really find anything that seemed to be related to the error that I'm having as I don't print out any output in my other threads. 我已经搜索过这个但是找不到任何与我所遇到的错误相关的东西,因为我没有打印出其他线程中的任何输出。

my plan was to have the threads chain start each other so that they don't happen to take one file twice and skip some other file. 我的计划是让线程链相互启动,这样它们就不会碰到两次文件并跳过其他文件。

thanks for any help! 谢谢你的帮助!

mylist = [list of filenames in here]

mycount = 0

def download():
    global mycount
    url = "http://myserver.com/content/files/" + mylist[mycount]
    myfile = urllib2.urlopen(url)
    with open(mylist[mycount],'wb') as output:
        output.write(myfile.read())


def chain():
    global mycount
    if mycount <= len(mylist) - 1:
        thread.start_new_thread( download, ())
        mycount = mycount + 1
        chain()

chain()

So I actually managed to make it work after some more googling and reading. 所以我真的设法让它在谷歌搜索和阅读后工作。

current code: 当前代码:

mycount = 0
mylistLoc = []
for index in range(len(mylist)):
    mylistLoc.append(index)

def download(mycount):
global mylistLoc
url = "http://myserver.com/content/zips/" + mylist[mycount]
try:
    myfile = urllib2.urlopen(url)
    with open(mylist[mycount],'wb') as output:
        output.write(myfile.read())
except:
    print "Could not open URL: " + url
mycount = mycount + 1
mylistLoc = [mycount]

from multiprocessing.dummy import Pool as ThreadPool 
pool = ThreadPool(50) 
pool.map(download, mylistLoc)

First of all I want to mention that the try: statement has nothing to do with the multithreading for all the newbies that may read this. 首先,我想提一下,try:语句与所有可能读取此内容的新手的多线程无关。

But I found this method on another stackoverflow thread that explains this a bit better. 但我在另一个stackoverflow线程上发现了这个方法,这解释了这个更好一点。

How to use threading in Python? 如何在Python中使用线程?

But basically if I understand this correctly, you first import all the necessary functions with the 但基本上如果我理解正确,你首先导入所有必要的功能

from multiprocessing.dummy import Pool as ThreadPool

And then you set how many threads you want active. 然后设置要激活的线程数。 For me 50 was good. 对我来说50很好。 But make sure to read up more on this as having too many can cause problems. 但请务必阅读更多内容,因为太多可能导致问题。

then the juice 然后果汁

pool.map(download, mylistLoc)

Basically, call pool map for every number in mylistLoc. 基本上,mylistLoc中每个数字的调用池映射。

I think it's a weird system but what do I know. 我认为这是一个奇怪的系统,但我知道什么。 It works. 有用。 Also, I'm a newbie myself. 另外,我自己也是新手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM