Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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多处理-TypeError:出于安全原因,不允许对AuthenticationString对象进行酸洗_Python_Python 3.x_Queue_Multiprocessing_Python Multiprocessing - Fatal编程技术网

Python多处理-TypeError:出于安全原因,不允许对AuthenticationString对象进行酸洗

Python多处理-TypeError:出于安全原因,不允许对AuthenticationString对象进行酸洗,python,python-3.x,queue,multiprocessing,python-multiprocessing,Python,Python 3.x,Queue,Multiprocessing,Python Multiprocessing,我有以下问题。我想实现一个网络爬虫,到目前为止,这是可行的,但它太慢了,我试图使用多处理来获取URL。 不幸的是,我在这个领域不是很有经验。 在阅读了一些之后,我觉得最简单的方法就是使用multiprocessing.pool中的map方法 但我经常会遇到以下错误: TypeError: Pickling an AuthenticationString object is disallowed for security reasons 我发现很少有案例有相同的错误,不幸的是,它们没有帮助我 我

我有以下问题。我想实现一个网络爬虫,到目前为止,这是可行的,但它太慢了,我试图使用多处理来获取URL。 不幸的是,我在这个领域不是很有经验。 在阅读了一些之后,我觉得最简单的方法就是使用multiprocessing.pool中的map方法

但我经常会遇到以下错误:

TypeError: Pickling an AuthenticationString object is disallowed for security reasons
我发现很少有案例有相同的错误,不幸的是,它们没有帮助我

我创建了一个剥离版本的代码,可以重现错误:

import multiprocessing

class TestCrawler:
    def __init__(self):
        self.m = multiprocessing.Manager()
        self.queue = self.m.Queue()
        for i in range(50):
            self.queue.put(str(i))
        self.pool = multiprocessing.Pool(6)



    def mainloop(self):
        self.process_next_url(self.queue)

        while True:
            self.pool.map(self.process_next_url, (self.queue,))                

    def process_next_url(self, queue):
        url = queue.get()
        print(url)


c = TestCrawler()
c.mainloop()
我将非常感谢任何帮助或建议

问题:但我经常会遇到以下错误:

TypeError: Pickling an AuthenticationString object is disallowed for security reasons
你所犯的错误是错误的,原因是

self.queue = self.m.Queue()
将队列实例化移到类TestCrawler之外。 这会导致另一个错误:

NotImplementedError:池对象不能在进程之间传递或pickle

原因是:

self.pool = multiprocessing.Pool(6)
这两个错误都表明pickle找不到类成员

注意:无限循环! 你跟随的while循环会导致一个无止境的循环! 这会使你的系统过载! 此外,你的池。地图。。。用一个任务只启动一个进程

我建议阅读

更改为:

class TestCrawler:
    def __init__(self, tasks):
        # Assign the Global task to class member
        self.queue = tasks
        for i in range(50):
            self.queue.put(str(i))

    def mainloop(self):
        # Instantiate the pool local
        pool = mp.Pool(6)
        for n in range(50):
            # .map requires a Parameter pass None
            pool.map(self.process_next_url, (None,))

    # None is passed
    def process_next_url(self, dummy):
        url = self.queue.get()
        print(url)

if __name__ == "__main__":
  # Create the Queue as Global
  tasks = mp.Manager().Queue()
  # Pass the Queue to your class TestCrawler
  c = TestCrawler(tasks)
  c.mainloop()
此示例启动5个进程,每个进程处理10个任务:

class TestCrawler2:
    def __init__(self, tasks):
        self.tasks = tasks

    def start(self):
        pool = mp.Pool(5)
        pool.map(self.process_url, self.tasks)

    def process_url(self, url):
        print('self.process_url({})'.format(url))

if __name__ == "__main__":
    tasks = ['url{}'.format(n) for n in range(50)]
    TestCrawler2(tasks).start()
用Python测试:3.4.2

问题:但我经常会遇到以下错误:

TypeError: Pickling an AuthenticationString object is disallowed for security reasons
你所犯的错误是错误的,原因是

self.queue = self.m.Queue()
将队列实例化移到类TestCrawler之外。 这会导致另一个错误:

NotImplementedError:池对象不能在进程之间传递或pickle

原因是:

self.pool = multiprocessing.Pool(6)
这两个错误都表明pickle找不到类成员

注意:无限循环! 你跟随的while循环会导致一个无止境的循环! 这会使你的系统过载! 此外,你的池。地图。。。用一个任务只启动一个进程

我建议阅读

更改为:

class TestCrawler:
    def __init__(self, tasks):
        # Assign the Global task to class member
        self.queue = tasks
        for i in range(50):
            self.queue.put(str(i))

    def mainloop(self):
        # Instantiate the pool local
        pool = mp.Pool(6)
        for n in range(50):
            # .map requires a Parameter pass None
            pool.map(self.process_next_url, (None,))

    # None is passed
    def process_next_url(self, dummy):
        url = self.queue.get()
        print(url)

if __name__ == "__main__":
  # Create the Queue as Global
  tasks = mp.Manager().Queue()
  # Pass the Queue to your class TestCrawler
  c = TestCrawler(tasks)
  c.mainloop()
此示例启动5个进程,每个进程处理10个任务:

class TestCrawler2:
    def __init__(self, tasks):
        self.tasks = tasks

    def start(self):
        pool = mp.Pool(5)
        pool.map(self.process_url, self.tasks)

    def process_url(self, url):
        print('self.process_url({})'.format(url))

if __name__ == "__main__":
    tasks = ['url{}'.format(n) for n in range(50)]
    TestCrawler2(tasks).start()

使用Python:3.4.2进行测试

非常感谢您的回答!它对这个简短的例子有效,但不知怎么的,当我在我的程序中以这种方式更改它时,我仍然有同样的问题。你能给我发些资料解释一下酸洗的原因吗?查找此文件时,我通常会收到有关pickle模块的文本。@blue:您的问题并显示您用于pool.map…,我假设您仍在尝试传递一个muliprocessing对象,这是不可能的!此代码仅适用于“map”方法,不适用于apply\u async。非常感谢您的回答!它对这个简短的例子有效,但不知怎么的,当我在我的程序中以这种方式更改它时,我仍然有同样的问题。你能给我发些资料解释一下酸洗的原因吗?查找此文件时,我通常会收到有关pickle模块的文本。@blue:您的问题并显示您用于pool.map…,我假设您仍在尝试传递一个muliprocessing对象,这是不可能的!此代码仅适用于“map”方法,而不适用于apply\u async