python中stop_thread_Python threading.Thread只能用私有方法self来停止.__ Thread_stop()
我有一个函数接受大量的x,y对作为输入,使用numpy和scipy做一些精细的曲线拟合,然后返回一个值.为了尝试加快速度,我尝试使用Queue.Queue将数据提供给两个线程.数据完成后.我试图让线程终止,然后结束调用进程并将控制权返回给
shell.
我试图理解为什么我必须在threading中使用私有方法.线程停止我的线程并将控制返回到命令行.
self.join()不会结束程序.获得控制权的唯一方法是使用私有停止方法.
def stop(self):
print "STOP CALLED"
self.finished.set()
print "SET DONE"
# self.join(timeout=None) does not work
self._Thread__stop()
这是我的代码的近似值:
class CalcThread(threading.Thread):
def __init__(self,in_queue,out_queue,function):
threading.Thread.__init__(self)
self.in_queue = in_queue
self.out_queue = out_queue
self.function = function
self.finished = threading.Event()
def stop(self):
print "STOP CALLED"
self.finished.set()
print "SET DONE"
self._Thread__stop()
def run(self):
while not self.finished.isSet():
params_for_function = self.in_queue.get()
try:
tm = self.function(paramsforfunction)
self.in_queue.task_done()
self.out_queue.put(tm)
except ValueError as v:
#modify params and reinsert into queue
window = params_for_function["window"]
params_for_function["window"] = window + 1
self.in_queue.put(params_for_function)
def big_calculation(well_id,window,data_arrays):
# do some analysis to calculate tm
return tm
if __name__ == "__main__":
NUM_THREADS = 2
workers = []
in_queue = Queue()
out_queue = Queue()
for i in range(NUM_THREADS):
w = CalcThread(in_queue,out_queue,big_calculation)
w.start()
workers.append(w)
if options.analyze_all:
for i in well_ids:
in_queue.put(dict(well_id=i,window=10,data_arrays=my_data_dict))
in_queue.join()
print "ALL THREADS SEEM TO BE DONE"
# gather data and report it from out_queue
for i in well_ids:
p = out_queue.get()
print p
out_queue.task_done()
# I had to do this to get the out_queue to proceed
if out_queue.qsize() == 0:
out_queue.join()
break
# Calling this stop method does not seem to return control to the command line unless I use threading.Thread private method
for aworker in workers:
aworker.stop()
python中stop_thread_Python threading.Thread只能用私有方法self来停止.__ Thread_stop()相关推荐
- Python threading Thread多线程的使用方法
Python threading Thread多线程的使用方法 参考资料:<Python 多线程>http://www.runoob.com/python/python-multithre ...
- python try语句例题_在Python中,一个try语句只能和一个except语句搭配。_学小易找答案...
[单选题]言语和语言不同,言语是_____. [填空题]901x28= [判断题]在Python中,类中定义的类的成员(属性)或类方法,默认情况下都为公有的. [单选题]Outlines can be ...
- 详解Python中pyautogui库的最全使用方法
这篇文章主要介绍了详解Python中pyautogui库的最全使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值 在使用Python做脚本的话,有两个库可以使用,一个 ...
- Python中pyautogui库的最全使用方法
Python中pyautogui库的最全使用方法 Python中pyautogui库的最全使用方法 常用操作 鼠标操作 键盘操作 弹窗操作 图像操作 参考资料 Python中pyautogui库的最全 ...
- python csv库,Python 中导入csv数据的三种方法
Python 中导入csv数据的三种方法,具体内容如下所示: 1.通过标准的Python库导入CSV文件: Python提供了一个标准的类库CSV文件.这个类库中的reader()函数用来导入CSV文 ...
- python调用shell命令-在Python中执行shell命令的6种方法,你都知道吗?
原标题:在Python中执行shell命令的6种方法,你都知道吗? Python经常被称作"胶水语言",因为它能够轻易地操作其他程序,轻易地包装使用其他语言编写的库.今天我们就讲解 ...
- python使用教程cmd啥意思-对python中执行DOS命令的3种方法总结
1. 使用os.system("cmd") 特点是执行的时候程序会打出cmd在Linux上执行的信息. import os os.system("ls") 2. ...
- python条件语句-Python中条件判断语句的简单使用方法
最简单的条件语句: if expression: expr_true_suite 如上,if是关键字,expression是条件表达式,条件表达式支持多重条件判断,可以用布尔操作符and.or和not ...
- python调用shell命令-python中执行shell命令的几个方法小结
最近有个需求就是页面上执行shell命令,第一想到的就是os.system, 复制代码 代码如下: os.system('cat /proc/cpuinfo') 但是发现页面上打印的命令执行结果 0或 ...
- 小结两种在Python中导入C语言扩展库的方法
小结两种在Python中导入C语言扩展库的方法 分类: Pythoner2009-08-18 20:44 2563人阅读 评论(1) 收藏 举报 python扩展c语言importstring 一种是 ...
最新文章
- python目标跟踪_商汤开源最大目标跟踪库PySOT,代码已正式上线!
- Macbook air 键盘标点符号怎么输出?
- mysql_upgrade --force_社区投稿 | MySQL 8.0.16 告别mysql_upgrade升级方式
- OPA 5 - CreateButtonTest creates CreateButtonSteps
- 值类型和引用类型的区别
- 云数据库RDS基础版的优势及适用场景
- java 0l是多少_Java 构造器 - osc_0ltyoebk的个人空间 - OSCHINA - 中文开源技术交流社区...
- linux搭建环境软raid5
- python数据变更邮件提醒_Python之Bilibili自动更新邮件提醒并任务栏图标「完整代码」...
- avl树 php,PHP实现平衡二叉树(AVL树)
- 洛杉矶儿童医院被列为美国西部第一,在全美儿童医院中的总体排名升至第五
- Windows 10系统【之】内置应用
- 字节 找出最近时间(回溯) C++ 2021-07-17
- 安卓获取不到imsi_Android10.0 SDK29版本拿不到手机的IMSI解决办法
- cisco 华三 对接_H3C交换机与Cisco交换机STP协议对接注意事项
- GitHub构建Maven依赖仓库
- 使用Android Studio调试系统应用之TvSettings(一):移植
- cr2.sh和phpupdate.out挖矿程序处理
- 数据库函数之日期函数
- 基于PHP音乐网站平台系统设计与实现 开题报告