我有一个python线程的问题。我已经四处看了一天多了,情况并没有好转,所以我想去寻求帮助。我使用python3.4。第一个问题是:
class myThread (threading.Thread):
def __init__(self, url):
threading.Thread.__init__(self)
self.url = url
def run(self):
spider (url)我在代码中的某个部分使用了toBeProcessed +'/robots.txt'。如果我使用上面的方法,它没有给我错误-but它仍然不像它应该的那样工作,并不是所有的线程都运行。而如果我使用下面的方法,它会告诉我它是unsupported operand type(s) for +: '_thread._local' and 'str'
def run(self):
spider (self.url)注意,我确实有这个声明toBeProcessed = threading.local()。
第二个问题是关于其余的代码,只有两个线程做工作,其余的线程-whatever是他们的数字不工作。
完整代码:
def spider(url,superMaxPages):
print(threading.current_thread())
toBeProcessed = threading.local()
data = threading.local()
parser = threading.local()
links = threading.local()
lock = threading.Lock()
writeLock = threading.Lock()
# Start from the beginning of our collection of pages to visit:
while 1:
if LinkParser.numVisited > maxPages:
print ('max pages reached')
break
lock.acquire()
try:
if not url:
time.sleep(0.01)
lock.release()
continue
print('to be processed ')
toBeProcessed = url.pop()
except:
print('threading error')
lock.release()
# In case we are not allowed to read the page.
rp = robotparser.RobotFileParser()
rp.set_url(toBeProcessed +'/robots.txt')
rp.read()
if not(rp.can_fetch("*", toBeProcessed)):
continue
LinkParser.visited.append(toBeProcessed)
LinkParser.numVisited += 1
writeLock.acquire()
try:
f.write(toBeProcessed+'\n')
finally:
writeLock.release()
try:
parser = LinkParser()
data, links = parser.getLinks(toBeProcessed)
# Add the pages that we visited to the end of our collection
url = url + links
print("One more page added from &i",threading.get_ident())
except:
print(" **Failed!**")
class myThread (threading.Thread):
def __init__(self, url, maxPages):
threading.Thread.__init__(self)
self.maxPages = maxPages
self.url = url
def run(self):
spider (self.url, maxPages)myThread( spider, (url,maxPages) ).start不是这样初始化的,我就是这样运行线程的。
https://stackoverflow.com/questions/36513505
复制相似问题