python捕捉執行緒錯誤 python 多執行緒錯誤

2021-10-11 16:45:09 字數 1933 閱讀 9257

我想用多執行緒查詢資料庫,然後進行資料操作。

_list = range(19999, 100000)

pool = threadpool(10)

results = pool.map(main, _list)

pool.close()

pool.join()

def main(i):

print(i)

# query id, link, keywords in db

res = query('select id,link,keywords from articles where id = {}'.format(i))

但是報錯如下,是資料庫扛不住這樣的查詢還是啥意思?

traceback (most recent call last):

file "main.py", line 60, in

results = pool.map(main, _list)

file "/usr/local/cellar/python3/3.5.1/frameworks/python.framework/versions/3.5/lib/python3.5/multiprocessing/pool.py", line 260, in map

return self._map_async(func, iterable, mapstar, chunksize).get()

file "/usr/local/cellar/python3/3.5.1/frameworks/python.framework/versions/3.5/lib/python3.5/multiprocessing/pool.py", line 608, in get

raise self._value

file "/usr/local/cellar/python3/3.5.1/frameworks/python.framework/versions/3.5/lib/python3.5/multiprocessing/pool.py", line 119, in worker

result = (true, func(*args, **kwds))

file "/usr/local/cellar/python3/3.5.1/frameworks/python.framework/versions/3.5/lib/python3.5/multiprocessing/pool.py", line 44, in mapstar

return list(map(*args))

file "main.py", line 17, in main

res = gao_query('select id,link,keywords from articles where id = {}'.format(i))

file "/users/ru/desktop/crawl_keywords/db.py", line 13, in query_with_desc

for res in cur.fetchall():

psycopg2.programmingerror: no results to fetch

(py3)

query函式如下: 返回資料以字典格式,keys為colomn名。

query = partial(query_with_desc, cur)

def query_with_desc(cur, sql):

cur: postgresl cursor

sql: sql statement

cur.execute(sql)

index = cur.description

result =

for res in cur.fetchall():

row = {}

for i in range(len(index)):

row[index[i][0]] = res[i]

return result

python錯誤捕捉

python使用try except捕捉錯誤,當發現錯誤時無需從底層向上依次返回錯誤 而是直接丟擲異常。通常使用方法 try 執行語句,發生錯誤時後續語句不在執行,轉至except except 處理該型別的錯誤 except else 無錯誤發生時執行此處 finally 不管有無錯誤都會執行 錯...

php 捕捉mysql錯誤 PHP捕捉錯誤

php捕捉錯誤 禁止錯誤輸出 error reporting 0 設定錯誤處理器 set error handler errorhandler 在指令碼結束時執行的函式 register shutdown function fatalerrorhandler 錯誤處理 param int err n...

php錯誤及異常捕捉

在實際開發中,錯誤及異常捕捉僅僅靠try catch 是遠遠不夠的。所以引用以下幾中函式。a set error handler 一般用於捕捉 e notice e user error e user warning e user notice 不能捕捉 e error,e parse,e core...