#!/usr/bin/python
-- coding: UTF-8 --
import thread
import time
为线程定义一个函数
def print_time( threadName, delay):
count = 0
while count < 5:
time.sleep(delay)
count += 1
print “%s: %s” % ( threadName, time.ctime(time.time()) )
创建两个线程
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
本文介绍了一个使用Python进行线程编程的实例,展示了如何定义线程函数,创建并启动多个线程,实现线程间的同步与并发执行。通过具体代码演示了线程的基本用法。

1303

被折叠的 条评论
为什么被折叠?



