python互斥锁、加锁、同步机制、异步通信知识总结

yipeiwu_com6年前Python基础

某个线程要共享数据时,先将其锁定,此时资源的状态为“锁定”,其他线程不能更改;直到该线程释放资源,将资源的状态变成“非锁定”,其他的线程才能再次锁定该资源。互斥锁保证了每次只有一个线程进入写入操作,从而保证了多线程情况下数据的正确性。

采用f_flag的方法效率低

创建锁

mutex=threading.Lock()

锁定

mutex.acquire([blocking])#里面可以加blocking(等待的时间)或者不加,不加就会一直等待(堵塞)

释放

mutex.release()

import threading 
from threading import Thread 
from threading import Lock 
import time 
 
thnum=0 
#两个线程都在抢着对这个锁进行上锁,如果有一方成功上锁,那么导致另外一方会堵塞(一直等待),到这个锁被解开为之 
class MyThread(threading.Thread): 
  def run(self): 
    mutex.acquire() 
    for i in range(10000): 
      global thnum 
      thnum+=1   
    print(thnum) 
    mutex.release()  
def test(): 
  global thnum 
  mutex.acquire() #等待可以上锁,通知而不是轮训,没有占用CPU 
  for i in range(10000): 
    thnum+=1 
  print(thnum) 
  mutex.release()#解锁 
mutex=Lock() 
if __name__=='__main__': 
  t=MyThread() 
  t.start() 
 
#创建一把互斥锁,默认是没有上锁的 
 
thn=Thread(target=test) 
thn.start() 
 
''''' 
10000 
20000 
''' 

只要一上锁,由多任务变为单任务,相当于只有一个线程在运行。

下面的代码相对上面加锁的时间变短了

import threading 
from threading import Thread 
from threading import Lock 
import time 
 
thnum=0 
#两个线程都在抢着对这个锁进行上锁,如果有一方成功上锁,那么导致另外一方会堵塞(一直等待),到这个锁被解开为之 
class MyThread(threading.Thread): 
  def run(self): 
    for i in range(10000): 
      mutex.acquire() 
      global thnum 
      thnum+=1 
      mutex.release()#释放后,都开始抢,这样上锁的时间变短  
    print(thnum) 
     
def test(): 
  global thnum 
  for i in range(10000): 
    mutex.acquire() 
    thnum+=1 
    mutex.release()#解锁 
  print(thnum) 
mutex=Lock() 
if __name__=='__main__': 
  t=MyThread() 
  t.start() 
 
#创建一把互斥锁,默认是没有上锁的 
 
thn=Thread(target=test) 
thn.start() 
 
''''' 
10000 
20000 
''' 

只有必须加锁的地方才加锁

同步:按照预定的先后顺序执行

一个运行完后,释放下一个,下一个锁定后运行,再释放下一个,下一个锁定后,运行后释放下一个..... 释放第一个

异步:

#异步的实现 
from multiprocessing import Pool 
import time 
import os 
 
#getpid()获取当前进程的进程号 
#getppid()获取当前进程的父进程号 
 
def test():#子进程 
  print("----进程池中的进程-----pid=%d,ppid=%d --"%(os.getpid(),os.getppid())) 
  for i in range(3): 
    print("-----%d----"%i) 
    time.sleep(1) 
  return "over" #子进程执行完后返回给操作系统,返回给父进程 
 
def test2(args): 
  print("-----callback func----pid=%d"%os.getpid())#主进程调用test2 
  print("------callback func---args=%s"%args) 
 
def main(): 
  pool=Pool(3) 
  pool.apply_async(func=test,callback=test2)#回调 
  time.sleep(5)#收到func进程结束后的信号后,执行回调函数test2 
 
  print("----主进程-pid = %d"%os.getpid()) 
 
if __name__=="__main__": 
  #main() 
  pool=Pool(3) 
  pool.apply_async(test,callback=test2)#回调 
  time.sleep(5)#收到func进程结束后的信号后,执行回调函数test2 
 
  print("----主进程-pid = %d"%os.getpid()) 
 
'''''显示结果不太正确,应该先运行test呀,再运行test2 
-----callback func----pid=7044 
------callback func---args=over 
----主进程-pid = 7044 
----进程池中的进程-----pid=3772,ppid=7044 -- 
-----0---- 
-----1---- 
-----2---- 
''' 

相关文章

python中__call__方法示例分析

本文实例讲述了python中__call__方法的用法,分享给大家供大家参考。具体方法分析如下: Python中的__call__允许程序员创建可调用的对象(实例),默认情况下, __c...

python元组和字典的内建函数实例详解

本文实例讲述了python元祖和字典的内建函数。分享给大家供大家参考,具体如下: 元组Tuple 元组是序列类型一种,也是不可变类型数据结构,对元组修改后会生成一个新的元组。所以Tupl...

Python迭代器iterator生成器generator使用解析

1. 迭代 根据记录的前面的元素的位置信息 去访问后续的元素的过程 -遍历 迭代 2. 可迭代对象 iterable 如何判断可迭代对象的3种方式 能够被迭代访问的对象 for...

shell命令行,一键创建 python 模板文件脚本方法

写 python 文件时,每个文件开头都必须注明版本和编码。每次我 touch 文件之后粘贴这两句话让我不胜其烦。 由于我没有安装 python 的 IDE 工具,也没有为 vim 安装...

Python模块搜索路径代码详解

简述 由于某些原因,在使用 import 时,Python 找不到相应的模块。这时,解释器就会发牢骚 - ImportError。 那么,Python 如何知道在哪里搜索模块的路径呢?...