Python 尝试在多线程中处理链接时出错
当我试图通过python3.4中的asyncio和concurrent.futures模块处理20个线程的100k url时,我遇到了这个错误。它出现后,就像2-5分钟的脚本工作Python 尝试在多线程中处理链接时出错,python,multithreading,python-3.x,asynchronous,python-asyncio,Python,Multithreading,Python 3.x,Asynchronous,Python Asyncio,当我试图通过python3.4中的asyncio和concurrent.futures模块处理20个线程的100k url时,我遇到了这个错误。它出现后,就像2-5分钟的脚本工作 concurrent.futures.process.BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending. Task exception was
concurrent.futures.process.BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending.
Task exception was never retrieved
future: <Task finished coro=<main() done, defined at async.py:173> exception=BrokenProcessPool('A process in the process pool was terminated abruptly while the future was running or pending.',)>
我试图优化我的代码,但仍然得到这个错误,前面已经描述过
代码:
在loop.close之后添加p.shutdown以等待完成所有执行的任务 仍然得到相同的错误。回溯到这一行:从loop.run\u in\u executorp,operation,item产生的收益现在有什么问题吗?啊。您在操作调用中得到未处理的异常。
import asyncio
import time
from concurrent.futures import ProcessPoolExecutor
from grab import Grab
import random
import psycopg2
# Open connection to the database
connection = psycopg2.connect(database="<....>",
user="<....>",
password="<....>",
host="127.0.0.1",
port="5432")
# Create a new cursor for it
c = connection.cursor()
# Select settings from database
c.execute("SELECT * FROM <....> WHERE id=1;")
data = c.fetchall()
# Get time starting script
start_time = time.time()
def operation(link):
# init grab framework
g = Grab()
# try to find some elements on the page
try:
# open link
g.go(link)
# some link processing
<....>
except:
pass
@asyncio.coroutine
def main(item):
yield from loop.run_in_executor(p, operation, item)
# Create async loop, declare number of threads
loop = asyncio.get_event_loop()
p = ProcessPoolExecutor(data[0][13]) # =20
# Init tasks list - empty
tasks = []
# Select all urls which need to process
c.execute ("SELECT url FROM <....> ORDER BY id;")
# Forming tasks
for item in c.fetchall():
tasks.append(main(item[0]))
# Close main connection to the database
connection.close()
# Run async tasks
loop.run_until_complete(asyncio.wait(tasks))
# Close loop
loop.close()
# Get script finish time
print("--- %s seconds ---" % (time.time() - start_time))