python多线程问题
时间:2011-12-14
来源:互联网
如下代码,执行后线程0可以执行,但是线程1不能被执行,什么问题?
其中两个线程都是向数据库中加载数据,执行时间都比较长,一般几十秒;
if __name__ == "__main__":
time.sleep(0.2)
thread.start_new_thread(l_thread0,())
time.sleep(0.2)
thread.start_new_thread(l_thread1,())
while True:
time.sleep(1)
其中两个线程都是向数据库中加载数据,执行时间都比较长,一般几十秒;
if __name__ == "__main__":
time.sleep(0.2)
thread.start_new_thread(l_thread0,())
time.sleep(0.2)
thread.start_new_thread(l_thread1,())
while True:
time.sleep(1)
作者: howema 发布时间: 2011-12-14
试过了能执行
Python code
E:\codes\komodoprj\python2>c:\python27\python.exe thread01.py
this is function ft1. Wed Dec 14 14:24:25 2011
this is main:this is function ft2. Wed Dec 14 14:24:25 2011
Wed Dec 14 14:24:25 2011
this is function ft1. Wed Dec 14 14:24:26 2011
this is function ft2. Wed Dec 14 14:24:26 2011
this is main: Wed Dec 14 14:24:26 2011
this is function ft1. Wed Dec 14 14:24:27 2011
this is function ft2. Wed Dec 14 14:24:27 2011
this is main: Wed Dec 14 14:24:27 2011
this is function ft1. Wed Dec 14 14:24:28 2011
this is function ft2. Wed Dec 14 14:24:28 2011
this is main: Wed Dec 14 14:24:28 2011
this is function ft1. Wed Dec 14 14:24:29 2011
this is function ft2. Wed Dec 14 14:24:29 2011
this is main: Wed Dec 14 14:24:29 2011
this is function ft1. Wed Dec 14 14:24:30 2011
this is function ft2. Wed Dec 14 14:24:30 2011
this is main: Wed Dec 14 14:24:30 2011
Traceback (most recent call last):
File "thread01.py", line 23, in <module>
time.sleep(1)
KeyboardInterrupt
E:\codes\komodoprj\python2>
Python code
#!/usr/bin/env python import thread,time def l_thread0(): while 1: print 'this is function ft1.',time.ctime() time.sleep(1) def l_thread1(): while 1: print 'this is function ft2.',time.ctime() time.sleep(1) if __name__ == "__main__": time.sleep(0.2) thread.start_new_thread(l_thread0,()) time.sleep(0.2) thread.start_new_thread(l_thread1,()) while True: print 'this is main:',time.ctime() time.sleep(1)
E:\codes\komodoprj\python2>c:\python27\python.exe thread01.py
this is function ft1. Wed Dec 14 14:24:25 2011
this is main:this is function ft2. Wed Dec 14 14:24:25 2011
Wed Dec 14 14:24:25 2011
this is function ft1. Wed Dec 14 14:24:26 2011
this is function ft2. Wed Dec 14 14:24:26 2011
this is main: Wed Dec 14 14:24:26 2011
this is function ft1. Wed Dec 14 14:24:27 2011
this is function ft2. Wed Dec 14 14:24:27 2011
this is main: Wed Dec 14 14:24:27 2011
this is function ft1. Wed Dec 14 14:24:28 2011
this is function ft2. Wed Dec 14 14:24:28 2011
this is main: Wed Dec 14 14:24:28 2011
this is function ft1. Wed Dec 14 14:24:29 2011
this is function ft2. Wed Dec 14 14:24:29 2011
this is main: Wed Dec 14 14:24:29 2011
this is function ft1. Wed Dec 14 14:24:30 2011
this is function ft2. Wed Dec 14 14:24:30 2011
this is main: Wed Dec 14 14:24:30 2011
Traceback (most recent call last):
File "thread01.py", line 23, in <module>
time.sleep(1)
KeyboardInterrupt
E:\codes\komodoprj\python2>
作者: askandstudy 发布时间: 2011-12-14
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28