我编写了一个使用python mulithreading库执行API调用的脚本。它通过巨大的利润来加速处理,因为瓶颈是网络,而不是我主机上的任何东西(输入某人说python不会在这里做真正的多线程)。
问题在于,有时当我运行脚本时,我收到此错误,我的脚本最终挂起/睡眠:
pthread_cond_wait: Resource busy
我不知道如何弄清楚为什么会这样。如何获得更多上下文来调试问题?我是否需要将打印语句放在一堆随机位置,并希望能够捕获导致此问题的任何问题?有没有更好的调试方法?
如果有帮助,这就是我实现多线程的方式:
for i in range(threads): # make the threads
t = threading.Thread(target=queue_worker, args=[apikey, q, retries, hit_threshold]) # The threads will use the "queue_worker" function with these parameters
t.daemon = True
t.start() # start the thread!
# Data is put onto the queue and queue_worker does the API work here...
...
q.join() # Clean up and close the threads when the threads are all idle (no more data on the queue)
编辑:
queue_worker,api和主要代码基本上是这样的:
def queue_worker(apikey, q, retries, hit_threshold)
api_data = q.get()
for x in range(retries)
try:
response = do_api(api_data, apikey)
except Exception as error:
time.sleep(5)
continue
else:
error_count = error_count + 1
q.task_done()
continue
#... data parsing code here...
#... printing parsed data to screen here if a particular value returned is greater than "hit_threshold"...
q.task_done()
def do_api(api_data, apikey)
params = { 'apikey': apikey, 'resource': api_data }
response = requests.get('https://MYURL.com/api', params=params, timeout=10)
return response
if __name__ == '__main__':
threads = 50
q = Queue.Queue(threads)
for i in range(threads): # make the threads
t = threading.Thread(target=queue_worker, args=[apikey, q, retries, hit_threshold]) # The threads will use the "queue_worker" function with these parameters
t.daemon = True
t.start() # start the thread!
# Data is put onto the queue and queue_worker does the API work here...
...
q.join() # Clean up and close the threads when the threads are all idle (no more data on the queue)
答案 0 :(得分:1)
评论:有关调试的提示吗?
Locks, Condition
或其他threading
函数对嵌套用法进行双重检查。 Locks
访问共享变量。阅读Python Threads and the Global Interpreter Lock 并尝试这种“解决” 还有其他方法可以加速GIL操作或避免它:
- call''time.sleep()'' - 设置''sys.setcheckinterval()'' - 以优化模式运行Python - 将进程密集型任务转储到C扩展中 - 使用子进程模块执行命令
可能,你正面临着Python GIL!
what-is-a-global-interpreter-lock-gil
其他一个线程有锁。
锁定的使用不一致。