需求: 多个子线程同时调用a函数处理数据,如何实现让a函数超时之后return或者raise?
1 signal不能在子线程中使用,pass
2 把函数扔到进程里,开销太大,pass
3 把函数扔到单独线程里,改编进程类另其可接受终止信号,再开另外一个线程做监控,不易实现,且个人认为杀掉进程对系统资源的风险很大.
有没有什么好的解决方案,或者已有类库里面的timeout参数都是如何实现的呢?gevent有没有什么好的线程终止方案?
参考:
http://stackoverflow.com/ques...
Do you mean that the function is always doing CPU-intensive computing tasks? You can split the task, and after executing a small task, check whether it times out, and return if it times out, otherwise continue.
Supplement:
Let’s do this
My output here is:
I don’t know if the scenario you are facing is like this. The specific problem needs to be analyzed in detail.
signal module:
Reference http://blog.sina.com.cn/s/blo...
First look at the possible blocking places in the function. If there is a timeout, add a timeout.