[英]'TypeError("can't pickle generator objects"): Concurrent.future versus Asyncio
我想从python列表中的每个对象同时执行相同的实例方法。
我创建了一个 DataPipe 类,用于下载页面并将结果存储在一个数组中。 然后,当我完成下载特定域感兴趣的链接时,我生成这些页面,然后从这些页面生成相应的项目。
代码几乎按预期工作,现在,我想同时从多个域下载。
class DownloadCommand(Command):
def __init__(self, domain):
self.domain = domain
self.request_config = {'headers': self.domain.get_header(), 'proxy': self.domain.get_proxy()}
self.data_pipe = DataPipe(command=self)
def execute(self):
# try:
for brand, start_urls in self.domain.start_url.items():
for start_url in start_urls:
# yield from self.data_pipe.get_item_divs(brand, start_url)
yield from self.data_pipe.get_item_divs(brand, start_url)`
目前,我正在按顺序执行此操作。
def scrape(self):
for domain in self.get_initial_domain_list():
yield from self.fetch_from_dom(domain)
def fetch_from_dom(self, domain):
self.set_current_domain(domain)
for start_url_values, brand, start_url in domain.command.execute():
for items in start_url_values:
yield [self.get_item_value(item_div) for item_div in items]
我尝试使用multiprocessing.pool.Pool
对该应用程序进行multiprocessing.pool.Pool
线程multiprocessing.pool.Pool
但它不适用于实例方法。 然后当我使用pathos.multiprocessing import ProcessingPool
它返回一个错误:
multiprocess.pool.MaybeEncodingError: Error sending result: '[<generator object fetch_from_dom at 0x7fa984814af0>]'. Reason: 'TypeError("can't pickle generator objects",)'
我想切换到asyncio
或concurrent.futures
但我不确定哪一个会更好地做我想做的事情,如果实际上可以在 python 中做到这一点(同时从列表中的对象执行实例方法) . 任何人都可以帮忙吗?
不能在 python 多处理中使用 selenium 导致它克隆内存。 您可以尝试避免使用更简单的线程。 但这是我的多处理解决方案
注意:self 是我的驱动程序,因为我在 Selenium 上实现了自定义类
#Exit function
def cleanup(self):
print("++cleanup()++")
try:
try:
self.close()
except Exception as e:
#print("except cleanup - 2 - self.close() -> %s" %e)
pass
try:
self.quit()
except Exception as e:
#print("except cleanup - 3 - self.quit() -> %s" %e)
pass
try:
self.dispose()
#print("Fake disabled dispose()")
except Exception as e:
#print("except cleanup - 4 - self.dispose() -> %s" %e)
pass
try:
self.service.process.send_signal(signal.SIGTERM)
except Exception as e:
#print("except cleanup - 1 - self.service.process.send_signal(signal.SIGTERM) -> %s" %e)
pass
except Exception as e:
print("Except - CLEANUP -> %s" %e)
#print(str(e))
pass
在脚本代码中
#Before start threads
browser.cleanup()
del browser
#Now start multiprocessing and instance browser on each subprocess
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.