0
点赞
收藏
分享

微信扫一扫

看漫画学Python 第十六章代码

左小米z 2022-03-23 阅读 113
python

16.2 线程模块threading

import threading
#当前线程对象
t = threading.current_thread()
#当前线程名
print(t.name)

#返回当前处于活动状态的线程个数
print(threading.active_count())

#当前主线程对象
t = threading.main_thread()
#主线程名
print(t.name)

16.3.1 自定义函数实现线程体

import threading
import time

#线程体函数
def thread_body():
	#当前线程对象
	t = threading.current_thread()
	for n in range(5):
		#当前线程名
		print("第{0}次执行线程{1}".format(n,t.name))
		#线程休眠
		time.sleep(2)
	print("线程{0}执行完成!".format(t.name))

#主线程
#创建线程对象t1
t1 = threading.Thread(target = thread_body)
#创建线程对象t2
t2 = threading.Thread(target = thread_body,name = "MyThread")
#启动线程t1
t1.start()
#启动线程t2
t2.start()

16.3.2 自定义线程类实现线程体

import threading
import time

class SmallThread(threading.Thread):
	def __init__(self,name = None):
		super().__init__(name = name)
	#线程体函数
	def run(self):
		#当前线程对象
		t = threading.current_thread()
		for n in range(5):
			#当前线程名
			print("第{0}次执行线程{1}".format(n,t.name))
			#线程休眠
			time.sleep(2)
		print("线程{0}执行完成!".format(t.name))

#主线程
#创建线程对象t1
t1 = SmallThread()
#创建线程对象t2
t2 = SmallThread(name = "MyThread")
#启动线程t1
t1.start()
#启动线程t2
t2.start()

16.4.1 等待线程结束

import threading
import time

#共享变量
value = []

#线程体函数
def thread_body():
	#当前线程对象
	print("t1子线程开始...")
	for n in range(2):
		print("t1子线程执行...")
		value.append(n)
		#线程休眠
		time.sleep(2)
	print("子线程结束。")

#主线程
print("主线程开始执行")
#创建线程对象
t1 = threading.Thread(target = thread_body)
#启动线程
t1.start()
#主线程被堵塞,等待线程结束
t1.join()
print("value = {0}".format(value))
print("主线程继续执行")

16.4.2 线程停止

import threading
import time

#线程停止变量
isrunning = True

#工作线程体函数
def workthread_body():
	while isrunning:
		#线程开始工作
		print("工作线程执行中")
		#线程休眠
		time.sleep(5)
	print("工作线程结束。")

#控制线程体函数
def controlthread_body():
	global isrunning
	while isrunning:
		#从键盘输入停止指令exit
		command = input("请输入停止指令:")
		if command == 'exit':
			isrunning = False
			print("控制线程结束。")

#主线程
#创建工作线程对象workthread
workthread = threading.Thread(target = workthread_body)
#启动线程workthread
workthread.start()

#创建控制线程对象controlthread
controlthread = threading.Thread(target = controlthread_body)
#启动线程controlthread
controlthread.start()

16.5 下载图片示例

import threading
import time
import urllib.request

#线程停止变量
isrunning = True

#工作线程体函数
def workthread_body():
	while isrunning:
		#线程开始工作
		print("工作线程执行下载任务")
		download()
		#线程休眠
		time.sleep(5)
	print('工作线程结束')

#控制线程体函数
def controlthread_body():
	global isrunning
	while isrunning:
		#从键盘输入停止指令exit
		command = input("请输入停止指令:")
		if command == 'exit':
			isrunning = False
			print("控制线程结束。")

def download():
	url = 'htt]://localhost:8080/NoteWebService/logo.png'
	req = urllib.request.Request(url) 
	with urllib.request.urlopen(url) as response:
		data = response.read()
		f_name = 'download.png'
		with open(f_name,'wb') as f:
			f.write(data)
			print("文件下载成功")

#主线程
#创建工作线程对象workthread
workthread = threading.Thread(target = workthread_body)
#启动线程workthread
workthread.start()

#创建控制线程对象controlthread
controlthread = threading.Thread(target = controlthread_body)
#启动线程controlthread
controlthread.start()
举报

相关推荐

0 条评论