当前位置: 移动技术网 > IT编程>脚本编程>Python > python time.sleep()-睡眠线程还是进程?

python time.sleep()-睡眠线程还是进程?

2019年07月09日  | 移动技术网IT编程  | 我要评论

南雅中学网站,非诚勿扰高光威,湖南常德卷烟厂

它会阻止线程。如果查看python源代码中的modules / timemodule.c,您会看到在调用中floatsleep(),睡眠操作的实质部分包含在py_begin_allow_threads和py_end_allow_threads块中,允许其他线程继续执行当前线程睡觉。你也可以用一个简单的python程序来测试它:

 

import time
from threading import thread

class worker(thread):
    def run(self):
        for x in xrange(0,11):
            print x
            time.sleep(1)

class waiter(thread):
    def run(self):
        for x in xrange(100,103):
            print x
            time.sleep(5)

def run():
    worker().start()
    waiter().start()

哪个会打印:

>>> thread_test.run()
0
100
>>> 1
2
3
4
5
101
6
7
8
9
10
102

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网