python:线程进阶
发布时间:2020-12-17 00:01:24 所属栏目:Python 来源:网络整理
导读:1,守护线程 threading ( 3 ( t = Thread(target=span style="color: #000000"func) t.setDaemon(True) span style="color: #008000"# span style="color: #008000" 进程设置守护进程 是一个属性 daemon = True span style="color: #000000"t.start() t2 = T
1,守护线程 threading (3(t = Thread(target=<span style="color: #000000">func)
t.setDaemon(True) <span style="color: #008000">#<span style="color: #008000"> 进程设置守护进程 是一个属性 daemon = True <span style="color: #000000">t.start() t2 = Thread(target=<span style="color: #000000">func) t2.start() t2.join() <span style="color: #008000">#<span style="color: #008000"> 等待t2结束 # 守护线程 守护进程 都是等待主进程或者主线程中的代码 执行完毕# t2 = Thread(target=func)# t2.start() ---> 代码执行完毕# 守护线程就结束了# 主线程还没结束 等待t2继续执行# t2执行完毕 主线程结束# t2 = Thread(target=func)# t2.start()# t2.join() # 等待t2结束 执行完这句话代码才执行完毕# t2线程执行完毕# 主线程中没有代码了,守护线程结束 2,锁 threading threading 2= n
time.sleep(0.01= temp-1
n = 100<span style="color: #000000">
lock =<span style="color: #000000"> Lock() t_lst =<span style="color: #000000"> [] <span style="color: #0000ff">for i <span style="color: #0000ff">in range(100<span style="color: #000000">): t = Thread(target=<span style="color: #000000">func) t.start() t_lst.append(t) [t.join() <span style="color: #0000ff">for t <span style="color: #0000ff">in<span style="color: #000000"> t_lst] <span style="color: #0000ff">print<span style="color: #000000">(n) <span style="color: #008000">#<span style="color: #008000"> GIL 不是锁数据 而是锁线程<span style="color: #008000"> <span style="color: #008000"> 在多线程中 特殊情况 仍然要加锁 对数据 3,死锁 threading threading = kz = RLock()
(%(%(%<span style="color: #0000ff">def<span style="color: #000000"> eat2(name):
m.acquire() <span style="color: #008000">#<span style="color: #008000"> 没有钥匙 <span style="color: #0000ff">print(<span style="color: #800000">'<span style="color: #800000">%s拿到菠萝蜜干了<span style="color: #800000">' %<span style="color: #000000"> name) time.sleep(1<span style="color: #000000">) kz.acquire() <span style="color: #0000ff">print(<span style="color: #800000">'<span style="color: #800000">%s拿到筷子了<span style="color: #800000">' %<span style="color: #000000"> name) <span style="color: #0000ff">print(<span style="color: #800000">'<span style="color: #800000">%s吃菠萝蜜干<span style="color: #800000">' %<span style="color: #000000"> name) kz.release() m.release() Thread(target=eat,args=('兔子',)).start()Thread(target=eat2,args=('rabbit',)).start()Thread(target=eat,args=('大橘',args=('Orange_Cat',)).start() 在不同的线程中 恰好要对这两个数据进行操作所以就出现了死锁。4,信号量 threading threading (% ( % sem = Semaphore(5)
i range(20 Thread(target=func,args=
5,事件 threading threading conn_mysql():
count = 1
e.is_set():
count>3(%+= 10.5)
()
<span style="color: #0000ff">def <span style="color: #000000"> check_conn():<span style="color: #800000">'''<span style="color: #800000"> 检测数据库服务器的连接是否正常 <span style="color: #800000">'''<span style="color: #000000"> time.sleep(random.randint(1,2)) <span style="color: #008000">#<span style="color: #008000"> 模拟连接检测的时间 e.set() <span style="color: #008000">#<span style="color: #008000"> 告诉事件的标志数据库可以连接 <span style="color: #000000"> e =<span style="color: #000000"> Event() check = Thread(target=<span style="color: #000000">check_conn) check.start() conn = Thread(target=<span style="color: #000000">conn_mysql) conn.start() 6,条件
( %<span style="color: #0000ff">if <span style="color: #800080">name == <span style="color: #800000">'<span style="color: #800000">main<span style="color: #800000">'<span style="color: #000000">:
con = threading.Condition() <span style="color: #008000">#<span style="color: #008000"> 条件 = 锁 + wait的功能 <span style="color: #0000ff">for i <span style="color: #0000ff">in range(10<span style="color: #000000">): t = threading.Thread(target=run,)) t.start()
7,计时器 threading <span style="color: #0000ff">def<span style="color: #000000"> hello():
<span style="color: #0000ff">print(<span style="color: #800000">"<span style="color: #800000">hello,world<span style="color: #800000">"<span style="color: #000000">) <span style="color: #0000ff">while True: <span style="color: #008000">#<span style="color: #008000"> 每隔一段时间要开启一个线程 t = Timer(10,hello) <span style="color: #008000">#<span style="color: #008000"> 定时开启一个线程,执行一个任务 <span style="color: #008000">#<span style="color: #008000"> 定时 : 多久之后 单位是s <span style="color: #008000">#<span style="color: #008000"> 要执行的任务 :函数名 t.start() 8,队列 = queue.PriorityQueue()
pq.put((1,1,15,2,(pq.get())
9,concurrent与线程池和回调函数 concurrent 1,3 n*
= futures.ThreadPoolExecutor(5)
thread_pool.map(funcname,range(10))
f_lst = i range(10= thread_pool.submit(funcname,i)
f f_lst:
(f.result())
thread_pool.submit(funcname,1).add_done_callback(call)
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- python – 加入正则表达式搜索的输出
- pip matplotlib报错equired packages can not be built解决
- Python使用map()迭代列表中的列表
- python – Google App Engine中模型的默认值
- python – 从JSON文件创建的pandas数据帧中的UnicodeDecode
- Python简单计算文件MD5值的方法示例
- python – 生成最佳二叉搜索树(Cormen)
- Python解决抛小球问题 求小球下落经历的距离之和示例
- 使用inferred_freq设置pandas.tseries.index.DatetimeIndex
- python – 如何修复AttributeError:模块’numpy’没有属性