我有一个函数接受大量的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()相关推荐

  1. Python threading Thread多线程的使用方法

    Python threading Thread多线程的使用方法 参考资料:<Python 多线程>http://www.runoob.com/python/python-multithre ...

  2. python try语句例题_在Python中,一个try语句只能和一个except语句搭配。_学小易找答案...

    [单选题]言语和语言不同,言语是_____. [填空题]901x28= [判断题]在Python中,类中定义的类的成员(属性)或类方法,默认情况下都为公有的. [单选题]Outlines can be ...

  3. 详解Python中pyautogui库的最全使用方法

    这篇文章主要介绍了详解Python中pyautogui库的最全使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值 在使用Python做脚本的话,有两个库可以使用,一个 ...

  4. Python中pyautogui库的最全使用方法

    Python中pyautogui库的最全使用方法 Python中pyautogui库的最全使用方法 常用操作 鼠标操作 键盘操作 弹窗操作 图像操作 参考资料 Python中pyautogui库的最全 ...

  5. python csv库,Python 中导入csv数据的三种方法

    Python 中导入csv数据的三种方法,具体内容如下所示: 1.通过标准的Python库导入CSV文件: Python提供了一个标准的类库CSV文件.这个类库中的reader()函数用来导入CSV文 ...

  6. python调用shell命令-在Python中执行shell命令的6种方法,你都知道吗?

    原标题:在Python中执行shell命令的6种方法,你都知道吗? Python经常被称作"胶水语言",因为它能够轻易地操作其他程序,轻易地包装使用其他语言编写的库.今天我们就讲解 ...

  7. python使用教程cmd啥意思-对python中执行DOS命令的3种方法总结

    1. 使用os.system("cmd") 特点是执行的时候程序会打出cmd在Linux上执行的信息. import os os.system("ls") 2. ...

  8. python条件语句-Python中条件判断语句的简单使用方法

    最简单的条件语句: if expression: expr_true_suite 如上,if是关键字,expression是条件表达式,条件表达式支持多重条件判断,可以用布尔操作符and.or和not ...

  9. python调用shell命令-python中执行shell命令的几个方法小结

    最近有个需求就是页面上执行shell命令,第一想到的就是os.system, 复制代码 代码如下: os.system('cat /proc/cpuinfo') 但是发现页面上打印的命令执行结果 0或 ...

  10. 小结两种在Python中导入C语言扩展库的方法

    小结两种在Python中导入C语言扩展库的方法 分类: Pythoner2009-08-18 20:44 2563人阅读 评论(1) 收藏 举报 python扩展c语言importstring 一种是 ...

最新文章

  1. python目标跟踪_商汤开源最大目标跟踪库PySOT,代码已正式上线!
  2. Macbook air 键盘标点符号怎么输出?
  3. mysql_upgrade --force_社区投稿 | MySQL 8.0.16 告别mysql_upgrade升级方式
  4. OPA 5 - CreateButtonTest creates CreateButtonSteps
  5. 值类型和引用类型的区别
  6. 云数据库RDS基础版的优势及适用场景
  7. java 0l是多少_Java 构造器 - osc_0ltyoebk的个人空间 - OSCHINA - 中文开源技术交流社区...
  8. linux搭建环境软raid5
  9. python数据变更邮件提醒_Python之Bilibili自动更新邮件提醒并任务栏图标「完整代码」...
  10. avl树 php,PHP实现平衡二叉树(AVL树)
  11. 洛杉矶儿童医院被列为美国西部第一,在全美儿童医院中的总体排名升至第五
  12. Windows 10系统【之】内置应用
  13. 字节 找出最近时间(回溯) C++ 2021-07-17
  14. 安卓获取不到imsi_Android10.0 SDK29版本拿不到手机的IMSI解决办法
  15. cisco 华三 对接_H3C交换机与Cisco交换机STP协议对接注意事项
  16. GitHub构建Maven依赖仓库
  17. 使用Android Studio调试系统应用之TvSettings(一):移植
  18. cr2.sh和phpupdate.out挖矿程序处理
  19. 数据库函数之日期函数
  20. 基于PHP音乐网站平台系统设计与实现 开题报告

热门文章

  1. 2022年3月18到5月18的思考
  2. 要留住优秀员工,除了钱,你还可以用什么?
  3. c51 c语言 16位二进制转换为bcd码,16位二进制转换为BCD码的C51汇编程序.doc
  4. CSAPP第二章家庭作业
  5. android动态设置渐变背景
  6. 普通蓝牙防丢器已过时,onn推出支持苹果 Find My 防丢器
  7. MathType求和符号中的黑三角该如何消除
  8. python元编程的书_C++模板元编程 PDF 超清版
  9. 漫谈“中间件”与国产化【转】
  10. UG二次开发GRIP显示图层