加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 12:54:06 所属栏目:Python 来源:网络整理
导读:下边内容是关于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) cou

下边内容是关于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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读