事件调度器
事件调度器:顾名思义,主要作用是调度事件。其实对我而言,这个更像是一个定时器。这里不给出过多的解释(内容本身不复杂,暂时没找到官方的解释,担心误人子弟)。直接上代码。展示结果。
函数
sched = scheduler() # 实例化调度器
sched.enter() # 加入事件
sched.run() # 运行调度器
代码
# -*-coding: utf-8 -*-
import time
def GetTime():
time_stamp = time.time() # 获取时间戳
local_time = time.localtime(time_stamp) # 时间戳转北京时间
return time.strftime("%Y-%m-%d %H:%M:%S", local_time) # 北京时间转特定格式
def SchedDisplay():
from sched import scheduler
def PrintTime(sched: scheduler): # 被调度的事件
delay = 1 # 调度时间间隔
priority = 2 # 优先级
sched.enter(delay=delay, priority=priority, action=PrintTime, argument=(sched,)) # 加入事件
print("sched", GetTime()) # 北京时间转特定格式
sched = scheduler() # 实例化调度器
PrintTime(sched, ) # 调度事件
sched.run() # 运行调度器
if __name__ == '__main__':
SchedDisplay()
解释说明:先实例化一个调度器sched,然后在PrintTime事件中使用sched.enter添加事件本身,达到一个循环调度的效果,最后用sched.run()运行调度器
执行结果
sched 2021-06-09 18:22:58
sched 2021-06-09 18:22:59
sched 2021-06-09 18:23:00
sched 2021-06-09 18:23:01
...
线程
进程
进程是资源(CPU、内存等)分配的基本单位,它是程序执行时的一个实例。程序运行时系统就会创建一个进程,并为它分配资源,然后把该进程放入进程就绪队列,进程调度器选中它的时候就会为它分配CPU时间,程序开始真正运行。
线程
线程是操作系统能够进行运算调度的最小单位。是CPU调度和分派的基本单位,每个线程都有独立的堆栈和局部变量。
对于单核CPU而言:多线程就是一个CPU在来回的切换,在交替执行。
对于多核CPU而言:多线程就是同时有多条执行路径在同时(并行)执行,每个核执行一个线程,多个核就有可能是一块同时执行的。
线程与进程的关系
线程被包含在进程之中,是进程中的实际运作单位。一个进程可以由很多个线程组成,线程间共享进程的所有资源。
一个正在运行的软件(如迅雷)就是一个进程,一个进程可以同时运行多个任务( 迅雷软件可以同时下载多个文件,每个下载任务就是一个线程),可以简单的认为进程是线程的集合。
函数
lock = threading.Lock() # 申请一把锁
th = threading.Thread(target=fun, args=(param,)) # 实例化线程
th.start() # 启动线程
多线程代码
# -*-coding: utf-8 -*-
import time
def GetTime():
time_stamp = time.time() # 获取时间戳
local_time = time.localtime(time_stamp) # 时间戳转北京时间
return time.strftime("%Y-%m-%d %H:%M:%S", local_time) # 北京时间转特定格式
def ThreadDisplay():
import threading
lock = threading.Lock() # 申请一把锁
time_stamp = GetTime() # 获取时间戳
thread_status = True
def thread_1():
nonlocal time_stamp # 声明变量 time_stamp 为上一层定义的变量
while thread_status:
with lock: # 为共享数据上锁,防止同时处理时产生错误
time_stamp = GetTime()
def thread_2():
times = 1
nonlocal thread_status
while thread_status:
time.sleep(1)
with lock:
# 只是使用这个变量,即只读不写。在局部内找不到时,会根据特定顺序从外部寻找这个变量。
print("thread", time_stamp)
times += 1
if times > 3:
thread_status = False
th1 = threading.Thread(target=thread_1, args=()) # 实例化线程,注意,这里只填函数名,不含(参数1, ...)
th2 = threading.Thread(target=thread_2, args=())
th1.start() # 启动线程
th2.start()
if __name__ == '__main__':
ThreadDisplay()
执行结果
thread 2021-06-09 18:23:23
thread 2021-06-09 18:23:24
thread 2021-06-09 18:23:25
调度器与线程结合
代码
# -*-coding: utf-8 -*-
import time
def GetTime():
time_stamp = time.time() # 获取时间戳
local_time = time.localtime(time_stamp) # 时间戳转北京时间
return time.strftime("%Y-%m-%d %H:%M:%S", local_time) # 北京时间转特定格式
def SchedDisplay():
from sched import scheduler
def PrintTime(sched: scheduler): # 被调度的事件
delay = 1 # 调度时间间隔
priority = 2 # 优先级
sched.enter(delay=delay, priority=priority, action=PrintTime, argument=(sched,)) # 加入事件
print("sched", GetTime()) # 北京时间转特定格式
sched = scheduler() # 实例化调度器
PrintTime(sched, ) # 调度事件
sched.run() # 运行调度器
def ThreadDisplay():
import threading
th = threading.Thread(target=SchedDisplay, args=()) # 实例化线程
th.daemon = True
th.start() # 启动线程
if __name__ == '__main__':
ThreadDisplay()
time.sleep(5)
执行结果
sched 2021-06-11 19:08:22
sched 2021-06-11 19:08:23
sched 2021-06-11 19:08:24
sched 2021-06-11 19:08:25
sched 2021-06-11 19:08:26