python如何避免死锁_Python多线程及死锁预防

线程(英语:thread)是操作系统能够进行运算调度的最小单位。

并发:指的是任务数比cpu核数多的时候,通过内核调度,实现用多个任务“一起”执行。

并行:指的是任务数小于等于cpu核数,即任务真的是一起执行的。

同步: 就是协同步调,按预定的先后次序进行运行。

互斥锁: 解决线程之间资源竞争不安全问题。

死锁: 多个线程获取多个锁,造成A需要B持有的锁,B需要A持有的锁。

防止死锁: 程序设计时要尽量避免, 添加超时时间,为程序中的每一个锁分配一个唯一的id。

同步源语:

当一个进程调用一个send原语时,在消息开始发送后,发送进程便处于阻塞状态,直至消息完全发送完毕,send原语的后继语句才能继续执行。当一个进程调用一个receive原语时,并不立即返回控制,而是等到把消息实际接收下来,并把它放入指定的接收区,才返回控制,继续执行该原语的后继指令。在这段时间它一直处于阻塞状态。上述的send和receive被称为同步通信原语或阻塞通信原语。

python多线程之间共享数据

全局变量

传递参数

使用queue

创建基本的线程

# Code to execute in an independent thread

import time

def countdown(n):

while n > 0:

print('T-minus', n)

n -= 1

time.sleep(5)

# Create and launch a thread

from threading import Thread

t = Thread(target=countdown, args=(10,))

t.start()

默认,主线程会等待子线程执行完之后执行。

Queue 线程之间通信

from queue import Queue

from threading import Thread

# A thread that produces data

def producer(out_q):

while True:

# Produce some data

...

out_q.put(data)

# A thread that consumes data

def consumer(in_q):

while True:

# Get some data

data = in_q.get()

# Process the data

...

# Create the shared queue and launch both threads

q = Queue()

t1 = Thread(target=consumer, args=(q,))

t2 = Thread(target=producer, args=(q,))

t1.start()

t2.start()

Queue 对象已经包含了必要的锁。

给原子性操作加锁

import threading

class SharedCounter:

'''

A counter object that can be shared by multiple threads.

'''

def __init__(self, initial_value = 0):

self._value = initial_value

self._value_lock = threading.Lock()

def incr(self,delta=1):

'''

Increment the counter with locking

'''

with self._value_lock:

self._value += delta

def decr(self,delta=1):

'''

Decrement the counter with locking

'''

with self._value_lock:

self._value -= delta

使用threading.Lock()可以得到一个互斥锁。

# 创建锁

mutex = threading.Lock()

# 锁定

mutex.acquire()

# 释放

mutex.release()

一个线程获取多个锁如何避免死锁

将获取锁的顺序规定,如,id(lock)从小到大排序。

import threading

from contextlib import contextmanager

import time

# Thread-local state to stored information on locks already acquired

_local = threading.local()

@contextmanager

def acquire(flag, *locks):

print(flag, 'start......................')

# Sort locks by object identifier

locks = sorted(locks, key=lambda x: id(x))

# Make sure lock order of previously acquired locks is not violated

acquired = getattr(_local, 'acquired', [])

if acquired and max(id(lock) for lock in acquired) >= id(locks[0]):

raise RuntimeError('Lock Order Violation')

# Acquire all of the locks

acquired.extend(locks)

print(flag, 'acquired:', acquired)

_local.acquired = acquired

try:

for lock in locks:

print(flag, 'start lock acquired for:', lock)

lock.acquire()

print(flag, 'end lock acquired for:', lock)

print(flag, 'end......................')

yield

finally:

# Release locks in reverse order of acquisition

for lock in reversed(locks):

lock.release()

del acquired[-len(locks):]

import threading

x_lock = threading.Lock()

y_lock = threading.Lock()

g_num = 0

def thread_1():

cur_tim = time.time()

FLAG = True

global g_num

while FLAG:

if time.time() - cur_tim > 1:

FLAG = False

with acquire('thread1', y_lock, x_lock):

print('Thread-1')

g_num += 1

def thread_2():

cur_tim = time.time()

FLAG = True

global g_num

while FLAG:

if time.time() - cur_tim > 1:

FLAG = False

with acquire('thread2',y_lock, x_lock):

print('Thread-2')

g_num += 1

t1 = threading.Thread(target=thread_1)

t1.start()

t2 = threading.Thread(target=thread_2)

t2.start()

time.sleep(2)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python 中,多线程编程是很常见的操作。但是多线程编程必然涉及到对共享资源的读写操作,而这很容易引起竞争条件,从而导致数据的不一致性。为了解决这个问题,Python 提供了 Lock 和 RLock 两种锁机制。 1. Lock Lock 是最简单的锁机制,也是最常用的锁机制。它通过 acquire() 方法获得锁,通过 release() 方法释放锁。当一个线程获得了锁,其他线程就不能再获得这个锁,直到这个线程释放锁为止。 下面是一个简单的例子: ```python import threading lock = threading.Lock() def func(): lock.acquire() print('Hello, world!') lock.release() t = threading.Thread(target=func) t.start() ``` 这个例子中,首先创建了一个 Lock 对象,然后定义了一个 func 函数,在函数中先获得锁,然后输出一句话,最后释放锁。在创建一个线程并执行这个函数时,输出的结果是: ``` Hello, world! ``` 在这个例子中,只有一个线程获得了锁,其他线程不能再获得这个锁,所以输出的结果只有一行。 2. RLock RLock 是可重入锁,它可以被一个线程多次 acquire(),而不会发生死锁。这个锁机制可以用在递归函数中,因为递归函数可能会多次调用自身。 下面是一个简单的例子: ```python import threading lock = threading.RLock() def func(): lock.acquire() print('Hello, world!') lock.acquire() print('Hello, again!') lock.release() lock.release() t = threading.Thread(target=func) t.start() ``` 这个例子中,首先创建了一个 RLock 对象,然后定义了一个 func 函数,在函数中先获得锁,输出一句话,再次获得锁,输出另一句话,最后释放锁。在创建一个线程并执行这个函数时,输出的结果是: ``` Hello, world! Hello, again! ``` 在这个例子中,虽然 func 函数中多次获得了锁,但是由于使用的是可重入锁,所以不会发生死锁,输出的结果也是正确的。 总结: Lock 和 RLock 都是 Python 中常用的锁机制,Lock 是最简单的锁机制,而 RLock 是可重入锁,可以在递归函数中使用。在多线程编程中,使用这两个锁机制可以有效地避免竞争条件,保证数据的一致性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值