微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python多线程模块thread使用范例的代码

下边内容是关于python多线程模块thread使用范例的内容,希望能对小伙伴们也有用处。
#!/usr/bin/python

import thread
import time

# Define a function for the thread
def print_time( threadName,delay):
count = 0
while count < 5:
time.sleep(delay)
count += 1
print "%s: %s" % ( threadName,time.ctime(time.time()) )

# Create two threads as follows
try:
thread.start_new_thread( print_time,("Thread-1",2,) )
thread.start_new_thread( print_time,("Thread-2",4,) )
except:
print "Error: unable to start thread"

while 1:
pass





输出结果如下:


Thread-1: Thu Jan 22 15:42:17 2009Thread-1: Thu Jan 22 15:42:19 2009Thread-2: Thu Jan 22 15:42:19 2009Thread-1: Thu Jan 22 15:42:21 2009Thread-2: Thu Jan 22 15:42:23 2009Thread-1: Thu Jan 22 15:42:23 2009Thread-1: Thu Jan 22 15:42:25 2009Thread-2: Thu Jan 22 15:42:27 2009Thread-2: Thu Jan 22 15:42:31 2009Thread-2: Thu Jan 22 15:42:35 2009

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐