【发布时间】:2013-08-09 17:13:41
【问题描述】:
最近我使用 python 和 redis 构建了一个 smarl messge-driven 项目。 我使用一个线程订阅redis通道(这里称为消息线程);定时器线程;和一个工作线程; 当消息线程收到足够的消息时,它会向工作人员发布一个任务。 我使用 redis-py 与 redis 通信
消息线程:
订阅redis;
while True:
get message;
if len(messages)>threashold: post task to Worker
工作线程:
while True:
wait task event;
do task; //this may be heavy
问题来了: 在这项工作一段时间后,redis-py subpub 被阻塞了!(ofcource redis 仍然是发布消息,但它不再返回,它只是被阻塞了!)。我使用 gdb 附加到它,我看到这样的堆栈帧:
[Switching to thread 4 (Thread 1084229984 (LWP 9812))]#0 0x000000302b80b0cf in __read_nocancel () from /lib64/tls/libpthread.so.0 (gdb) BT 0 0x000000302b80b0cf in __read_nocancel () from /lib64/tls/libpthread.so.0
1 0x00000000004e129a in posix_read(self=Variable "self" is not available.) at./Modules/posixmodule.c:6592
2 0x00000000004a04c5 in PyEval_EvalFrameEx (f=0x157a8c0, throwflag=Variable "throwflag" is not available.) at Python/ceval.c:4323
我什至使用 redis 'client kill' 命令来终止 python 和 redis 之间的连接,但是 python 仍然阻塞在那里,永远不会返回或引发异常。唯一的方法是杀死python进程使用kill -9。
然后我评论了工作的“做任务”过程(记住这个任务很重,它使网络 io、cpu 计算繁重),它运行良好,没有任何问题。
所以,似乎得出了结论:一旦我使用 worker 执行任务,消息线程将在套接字读取时阻塞。
怎么会这样!!
【问题讨论】: