本篇文章以一个简单的代碼例子示范如何定义函数以及使用函数
同时示范简单的线程使用方式
# -*- coding: UTF-8 -*-
# Description: Python thread practice code
import _thread
import time
def print_time(thread_name, delay):
count = 0
while count < 5:
count += 1
print("%s: %s" % (thread_name, time.ctime(time.time())))
time.sleep(delay)
try:
_thread.start_new_thread(print_time, ("Thread-1", 2))
_thread.start_new_thread(print_time, ("Thread-2", 5))
except:
print("Unable to start thread!")
while 1:
pass
第8行至第13行定义了一个名为print_time的函数,呼叫函数的方式则如同在C语言中的做法
在代码第16行和第17行是以线程来运行print_time函数