sleep() 函数的语法规则如下所示:sleep() 函数位于 time 模块中,因此在使用前,需先引入 time 模块。
time.sleep(secs)
其中,secs 参数用于指定暂停的秒数,
import threading
import time
#定义线程要调用的方法,*add可接收多个以非关键字方式传入的参数
def action(*add):
for arc in add:
#暂停 0.1 秒后,再执行
time.sleep(0.1)
#调用 getName() 方法获取当前执行该程序的线程名
print(threading.current_thread().getName() +" "+ arc)
#定义为线程方法传入的参数
my_tuple = ("http://c.biancheng.net/python/", "http://c.biancheng.net/shell/", "http://c.biancheng.net/java/")
#创建线程
thread = threading.Thread(target = action,args =my_tuple)
#启动线程
thread.start()
#主线程执行如下语句
for i in range(5):
print(threading.current_thread().getName())
程序执行结果为:
MainThread
MainThread
MainThread
MainThread
MainThread
Thread-1 http://c.biancheng.net/python/
Thread-1 http://c.biancheng.net/shell/
Thread-1 http://c.biancheng.net/java/
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有