"Can't open a new thread" Heroku 上的错误



我正在编写一个用于web抓取的Python脚本,并试图实现一种多线程方法来更快地完成工作。我使用ThreadPoolExecutor来设置线程的最大数量。

from threading import current_thread
from concurrent.futures import ThreadPoolExecutor, as_completed
MAX_THREADS = 100
def process_user(user):
# makes one or more network requests and returns the results
return {"count": 123}
users = get_users_from_database() # gets user records from the database
with ThreadPoolExecutor(max_workers=MAX_THREADS) as executor:
futures = [executor.submit(process_user, row) for row in users]
batch = []
for index, future in enumerate(as_completed(futures)):
result = future.result()
batch.append(result)
# store full batch or when there are no more left to store...
if (len(batch) >= BATCH_SIZE) or (index + 1 >= len(futures)):
insert_users(batch) # stores in the database
batch = []

Heroku表示,他们的免费层dyno最多可以运行256个线程。

然而,当我在免费层的Heroku服务器上运行该脚本时,它似乎可以用多达10个线程运行,但当尝试用更多线程运行时,该脚本只会遇到"RuntimeError:无法启动新线程"错误。

是否可以使用10根以上的螺纹?我需要升级层吗?谢谢你的建议。

似乎是内存问题。放大dynos可以使用更多的线程。

最新更新