Python论坛  - 讨论区

标题:Re: Re: [python-chinese] 关于线程内部死循环的问题

2006年02月17日 星期五 12:00

charles huang hyy at fjii.com
Fri Feb 17 12:00:50 HKT 2006

limodou,您好!

	又测试了在父线程中进行死循环,也可以正常的释放锁,让其它线程运行。测试了如下程序:
#!/usr/bin/env python
import Tkinter, threading, time

class Myprint(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        time_str=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        name_str = self.getName()
        while 1:
            time.sleep(1)
            print 'Client:' + name_str +' ' + time_str

client_t = Myprint()
client_t.start()
time.sleep(5)

root = Tkinter.Tk()
root.mainloop()

在窗口起来后,打印还是正常进行。说明tk的主窗口有把锁给释放了,不过,看过一个帖子,好像gtk的就不行,主窗口一起来,打印就中止了,锁好像没有释放。

======= 2006-02-17 11:09:52 您在来信中写道:=======

>On 2/17/06, charles huang <hyy at fjii.com> wrote:
>> 写了段代码,如下,运行起来还是符合我原先的预期的。把死循环放在最后生成的线程中运行,整个程序的运行还是正常的。不晓得窗口程序,最后调用的loop之类的函数,是什么个机理。
>>
>> #!/usr/bin/env python
>> import threading, time
>>
>> class Myprint(threading.Thread):
>>     def __init__(self,main_name):
>>         self.f_name = main_name
>>         threading.Thread.__init__(self)
>>
>>     def run(self):
>>         time_str=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
>>         name_str = self.getName()
>>         while 1:
>>             time.sleep(1)
>>             print 'Main:' + self.f_name + '  Client:' + name_str +' ' + time_str
>>
>> class Mythread(threading.Thread):
>>     def __init__(self):
>>         threading.Thread.__init__(self)
>>
>>     def run(self):
>>         name_str = self.getName()
>>         for j in range(1, 5):
>>             print 'Main:' + name_str
>>             Myclient = Myprint(name_str)
>>             Myclient.start()
>>
>> for i in range(1, 5):
>>         Mymain = Mythread()
>>         Mymain.start()
>>
>>
>>
>>>> 礼!
>>
>>
>> charles huang
>> 2006-02-17
>>
>
>窗口程序是一个事件循环处理系统。没有事件时它就循环,有事件时就分发。
>
>--
>I like python!
>My Blog: http://www.donews.net/limodou
>NewEdit Maillist: http://groups.google.com/group/NewEdit
>_______________________________________________
>python-chinese
>Post: send python-chinese at lists.python.cn
>Subscribe: send subscribe to python-chinese-request at lists.python.cn
>Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
>Detail Info: http://python.cn/mailman/listinfo/python-chinese

= = = = = = = = = = = = = = = = = = = =
			

        致
礼!
 
				 
        charles huang
        hyy at fjii.com
			   TEL: 0591-3333169-212
          2006-02-17


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年02月17日 星期五 12:09

limodou limodou at gmail.com
Fri Feb 17 12:09:58 HKT 2006

On 2/17/06, charles huang <hyy at fjii.com> wrote:
> limodou,您好!
>
>         又测试了在父线程中进行死循环,也可以正常的释放锁,让其它线程运行。测试了如下程序:
> #!/usr/bin/env python
> import Tkinter, threading, time
>
> class Myprint(threading.Thread):
>     def __init__(self):
>         threading.Thread.__init__(self)
>
>     def run(self):
>         time_str=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
>         name_str = self.getName()
>         while 1:
>             time.sleep(1)
>             print 'Client:' + name_str +' ' + time_str
>
> client_t = Myprint()
> client_t.start()
> time.sleep(5)
>
> root = Tkinter.Tk()
> root.mainloop()
>
> 在窗口起来后,打印还是正常进行。说明tk的主窗口有把锁给释放了,不过,看过一个帖子,好像gtk的就不行,主窗口一起来,打印就中止了,锁好像没有释放。
>
子线程应该放到事件循环中,而不是独立出来。你的程序线程与窗口无关,可能会有冲突。比如在启动了主窗体之后,再执行线程。

--
I like python!
My Blog: http://www.donews.net/limodou
NewEdit Maillist: http://groups.google.com/group/NewEdit

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号