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---- 
''' 

相关文章

Windows下Eclipse+PyDev配置Python+PyQt4开发环境

Windows下Eclipse+PyDev配置Python+PyQt4开发环境

本文为大家分享了Windows下配置Python PyQt4开发环境的详细步骤,供大家参考,具体内容如下 1. 下载相关软件 Eclipse下载地址:http://www.eclipse...

Django框架安装方法图文详解

Django框架安装方法图文详解

本文实例讲述了Django框架安装方法。分享给大家供大家参考,具体如下: Window 下安装 Django 如果你还未安装Python环境需要先下载Python安装包。 1、Pytho...

详解python单元测试框架unittest

一:unittest是python自带的一个单元测试框架,类似于java的junit,基本结构是类似的。 基本用法如下: 1.用import unittest导入unittest模块...

Python(PyS60)实现简单语音整点报时

本文实例为大家分享了python语音整点报时的具体代码,供大家参考,具体内容如下 主要的技术特殊点在于PyS60的定时器最多只能定2147秒。在手机上直接写的。 import e...

python获取当前文件路径以及父文件路径的方法

python获取当前文件路径以及父文件路径的方法

#当前文件的路径 pwd = os.getcwd() #当前文件的父路径 father_path=os.path.abspath(os.path.dirname(pwd)+os.p...