码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-07-29 14:11:15      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:erro   代码   tar   creat   err   for   start   function   用处   

下边内容是关于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 2009
Thread-1: Thu Jan 22 15:42:19 2009
Thread-2: Thu Jan 22 15:42:19 2009
Thread-1: Thu Jan 22 15:42:21 2009
Thread-2: Thu Jan 22 15:42:23 2009
Thread-1: Thu Jan 22 15:42:23 2009
Thread-1: Thu Jan 22 15:42:25 2009
Thread-2: Thu Jan 22 15:42:27 2009
Thread-2: Thu Jan 22 15:42:31 2009
Thread-2: Thu Jan 22 15:42:35 2009




 

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

标签:erro   代码   tar   creat   err   for   start   function   用处   

原文地址:https://www.cnblogs.com/gdszlg/p/11263456.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!