我正在尝试解析一堆链接并将解析的数据附加到sqlite3。我收到 sqlite3 数据库被锁定的错误,所以也许是因为我使用的池值太高?我试图将其降低到 5,但仍然收到如下所示的错误。
我的代码基本上看起来像这样:
from multiprocessing import Pool
with Pool(5) as p:
p.map(parse_link, links)
我的真实代码如下所示:
with Pool(5) as p:
p.map(Get_FT_OU, file_to_set('links.txt'))
# Where Get_FT_OU(link) appends links to a sqlite3 database.
当代码运行时,我经常收到这些错误。有人可以帮我修复它吗?
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/multiprocessing/pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/multiprocessing/pool.py", line 44, in mapstar
return list(map(*args))
File "/Users/christian/Documents/GitHub/odds/CP_Parser.py", line 166, in Get_FT_OU
cursor.execute(sql_str)
sqlite3.OperationalError: database is locked
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/christian/Documents/GitHub/odds/CP_Parser.py", line 206, in <module>
p.map(Get_FT_OU, file_to_set('links.txt'))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/multiprocessing/pool.py", line 266, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/multiprocessing/pool.py", line 644, in get
raise self._value
sqlite3.OperationalError: database is locked
>>>
我可以在不使用多处理的情况下很好地运行代码,实际上在使用 Pool(2( 时也没有收到任何错误,但是如果我走得更高,我就会得到这些错误。我使用的是最新的MacBook Air。
它以某种方式通过向连接添加超时= 10来工作
conn = sqlite3.connect(DB_FILENAME, timeout=10)