【Py】Python的调包日常——多线程篇

启动多线程

import threading

def change_model(app):
    print("change start")
    
t1 = threading.Thread(target=change_model, args=(app,))
t1.start()

锁有两种,一种是只能锁一次的锁threading.Lock(),另一种是可以锁多次的锁threading.RLock()

# 申请一个锁
app.lock = threading.Lock()
# 上锁
app.model_lock.acquire()
# 解锁
app.model_lock.release()

同步通讯

wait、notify

# 申请一个condition(RLock)
app.using_old_model = threading.Condition()
# 对condition加锁
app.using_old_model.acquire()
# wait
app.using_old_model.wait()

# notify
app.using_old_model.acquire()
app.using_old_model.notify()
app.using_old_model.release()

优雅结束

原理

signal模块中

signal.SIGINT

Interrupt from keyboard (CTRL + C).

Default action is to raise KeyboardInterrupt.

posted @ 2020-03-17 19:39  Ryan_W  阅读(182)  评论(0编辑  收藏  举报