首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python线程如何运行它的参数?

python线程如何运行它的参数?
EN

Stack Overflow用户
提问于 2016-04-09 14:27:31
回答 1查看 63关注 0票数 0

我有一个python线程的问题。我已经四处看了一天多了,情况并没有好转,所以我想去寻求帮助。我使用python3.4。第一个问题是:

代码语言:javascript
复制
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'

代码语言:javascript
复制
def run(self):
    spider (self.url)

注意,我确实有这个声明toBeProcessed = threading.local()

第二个问题是关于其余的代码,只有两个线程做工作,其余的线程-whatever是他们的数字不工作。

完整代码:

代码语言:javascript
复制
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不是这样初始化的,我就是这样运行线程的。

EN

回答 1

Stack Overflow用户

发布于 2016-04-09 14:47:46

你做错了。您不能将local的实例与字符串连接起来。您需要将属性存储在local实例上,如下所示:

代码语言:javascript
复制
import threading

toBeProcessed = threading.local()
toBeProcessed.url = url.pop()     
toBeProcessed.url += '/robots.txt'

此外,您还应该考虑在__init__方法中使用super()

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36513505

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档