Python “如何调试”;pthread_cond_wait:Resource busy";?
我编写了一个使用python mulithreading库执行API调用的脚本。它极大地加快了处理速度,因为瓶颈是网络,而不是主机上的任何东西(这里有人说python不做真正的多线程) 问题是,有时当我运行脚本时,我会收到此错误,而我的脚本最终会挂起/休眠:Python “如何调试”;pthread_cond_wait:Resource busy";?,python,multithreading,pthreads,Python,Multithreading,Pthreads,我编写了一个使用python mulithreading库执行API调用的脚本。它极大地加快了处理速度,因为瓶颈是网络,而不是主机上的任何东西(这里有人说python不做真正的多线程) 问题是,有时当我运行脚本时,我会收到此错误,而我的脚本最终会挂起/休眠: pthread_cond_wait: Resource busy 我不知道怎么弄清楚为什么会发生这种事。如何获得更多上下文来调试问题?我是否需要把打印的语句放在一堆随机的地方,并希望抓住导致这种情况的任何问题?有更好的调试方法吗 如果有帮
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)
评论:有关于调试的提示吗
锁、条件
或其他线程
函数对代码进行双重检查,以了解嵌套的用法李>
锁
访问共享的变量还有其他加速或避免GIL操纵的方法:
- 调用''time.sleep()''-设置''sys.setcheckinterval()''-以优化模式运行Python-将流程密集型任务转储到C扩展中-使用子流程模块执行命令
很可能,您正面临Python GIL 另一个线程有锁。
锁定的使用不一致。显示您的
队列\u工作人员
并解释apikey、q、重试、命中阈值
。确定完成。我不得不保持它的半模糊性,但希望这能有所帮助。稍后我将尝试使用它进行传统调试。这很难,因为错误只会偶尔发生,而且只有在经过几个小时的分析之后才会发生。@Thisisstackoverflow您得到了解决方案了吗?好的,很有趣。有关于调试的提示吗?