+ -
当前位置:首页 → 问答吧 → python多线程问题

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)

作者: howema   发布时间: 2011-12-14

试过了能执行

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