怎么用Python模拟死锁

发布时间:2022-10-10 17:19:51 作者:iii
来源:亿速云 阅读:103

这篇“怎么用Python模拟死锁”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“怎么用Python模拟死锁”文章吧。

前言

一个例子是在银行账户上:假如要在两个银行账户之间执行交易,你必须确保两个账户都被锁定,不受其他交易的影响,以达到正确的资金转移量。在这里,这个类比并不完全成立--哲学家对应的是锁定账户的交易(分叉)--但同样的技术困难也会出现。

其他的例子包括电商秒杀系统,多个用户抢一个商品,不允许一个数据库被多个客户同时修改。

死锁也是由一个并发程序需要同时具备的条件来定义的,这样才会发生死锁。这些条件是由计算机科学家Edward G. Coffman, Jr .首先提出的,因此被称为 Coffman 条件。这些条件如下:

造成线程死锁的常见例子包括:

  1. 一个在自己身上等待的线程(例如,试图两次获得同一个互斥锁)

  2. 互相等待的线程(例如,A 等待 B,B 等待 A)

  3. 未能释放资源的线程(例如,互斥锁、信号量、屏障、条件、事件等)

  4. 线程以不同的顺序获取互斥锁(例如,未能执行锁排序)

模拟死锁1:线程等待本身

导致死锁的一个常见原因是线程在自己身上等待。

我们并不打算让这种死锁发生,例如,我们不会故意写代码,导致线程自己等待。相反,由于一系列的函数调用和变量的传递,这种情况会意外地发生。

一个线程可能会因为很多原因而在自己身上等待,比如:

开发一个 task() 函数,直接尝试两次获取同一个 mutex 锁。也就是说,该任务将获取锁,然后再次尝试获取锁。

# task to be executed in a new thread

def task(lock):

    print('Thread acquiring lock...')

    with lock:

        print('Thread acquiring lock again...')

        with lock:

            # will never get here

            pass

这将导致死锁,因为线程已经持有该锁,并将永远等待自己释放该锁,以便它能再次获得该锁, task() 试图两次获取同一个锁并触发死锁。

在主线程中,可以创建锁:

# create the mutex lock
lock = Lock()

然后我们将创建并配置一个新的线程,在一个新的线程中执行我们的 task() 函数,然后启动这个线程并等待它终止,而它永远不会终止。

# create and configure the new thread
thread = Thread(target=task, args=(lock,))
# start the new thread
thread.start()
# wait for threads to exit...
thread.join()

完整代码如下:

from threading import Thread
from threading import Lock

# task to be executed in a new thread
def task(lock):
    print('Thread acquiring lock...')
    with lock:
        print('Thread acquiring lock again...')
        with lock:
            # will never get here
            pass

# create the mutex lock
lock = Lock()
# create and configure the new thread
thread = Thread(target=task, args=(lock,))
# start the new thread
thread.start()
# wait for threads to exit...
thread.join()

运行结果如下:

怎么用Python模拟死锁

首先创建锁,然后新的线程被混淆并启动,主线程阻塞,直到新线程终止,但它从未这样做。

新线程运行并首先获得了锁。然后它试图再次获得相同的互斥锁并阻塞。

它将永远阻塞,等待锁被释放。该锁不能被释放,因为该线程已经持有该锁。因此,该线程已经陷入死锁。

该程序必须被强制终止,例如,通过 Control-C 杀死终端。

模拟死锁2:线程互相等待

一个常见的例子就是两个或多个线程互相等待。例如:线程 A 等待线程 B,线程 B 等待线程 A。

如果有三个线程,可能会出现线程循环等待,例如:

怎么用Python模拟死锁

如果你设置了线程来等待其他线程的结果,这种死锁是很常见的,比如在一个流水线或工作流中,子任务的一些依赖关系是不符合顺序的。

from threading import current_thread
from threading import Thread
 
# task to be executed in a new thread
def task(other):
    # message
    print(f'[{current_thread().name}] waiting on [{other.name}]...\n')
    other.join()
 

# get the current thread
main_thread = current_thread()
# create the second thread
new_thread = Thread(target=task, args=(main_thread,))
# start the new thread
new_thread.start()
# run the first thread
task(new_thread)

首先得到主线程的实例 main_thread,然后创建一个新的线程 new_thread,并调用传递给主线程的 task() 函数。新线程返回一条信息并等待主线程停止,主线程用新线程的实例调用 task()函数,并等待新线程的终止。每个线程都在等待另一个线程终止,然后自己才能终止,这导致了一个死锁。

运行结果:

[Thread-1] waiting on [MainThread]...
[MainThread] waiting on [Thread-1]...

模拟死锁3:以错误的顺序获取锁

导致死锁的一个常见原因是,两个线程同时以不同的顺序获得锁。例如,我们可能有一个受锁保护的关键部分,在这个关键部分中,我们可能有代码或函数调用受第二个锁保护。

可能会遇到这样的情况:一个线程获得了锁 1 ,然后试图获得锁 2,然后有第二个线程调用获得锁 2 的功能,然后试图获得锁 1。如果这种情况同时发生,线程 1 持有锁 1,线程 2 持有锁 2,那么就会有一个死锁。

# example of a deadlock caused by acquiring locks in a different order
from time import sleep
from threading import Thread
from threading import Lock

# task to be executed in a new thread
def task(number, lock1, lock2):
    # acquire the first lock
    print(f'Thread {number} acquiring lock 1...')
    with lock1:
        # wait a moment
        sleep(1)
        # acquire the next lock
        print(f'Thread {number} acquiring lock 2...')
        with lock2:
            # never gets here..
            pass

# create the mutex locks
lock1 = Lock()
lock2 = Lock()
# create and configure the new threads
thread1 = Thread(target=task, args=(1, lock1, lock2))
thread2 = Thread(target=task, args=(2, lock2, lock1))
# start the new threads
thread1.start()
thread2.start()
# wait for threads to exit...
thread1.join()
thread2.join()

运行这个例子首先创建了两个锁。然后两个线程都被创建,主线程等待线程的终止。

第一个线程接收 lock1 和 lock2 作为参数。它获得了锁 1 并 sleep。

第二个线程接收 lock2 和 lock1 作为参数。它获得了锁 2 并 sleep。

第一个线程醒来并试图获取锁 2,但它必须等待,因为它已经被第二个线程获取。第二个线程醒来并试图获取锁 1,但它必须等待,因为它已经被第一个线程获取。

结果是一个死锁:

Thread 1 acquiring lock 1...
Thread 2 acquiring lock 1...
Thread 1 acquiring lock 2...
Thread 2 acquiring lock 2...

解决办法是确保锁在整个程序中总是以相同的顺序获得。这就是所谓的锁排序。

模拟死锁4:锁未释放

导致死锁的另一个常见原因是线程未能释放一个资源。这通常是由线程在关键部分引发错误或异常造成的,这种方式会阻止线程释放资源,包括:

# example of a deadlock caused by a thread failing to release a lock
from time import sleep
from threading import Thread
from threading import Lock

# task to be executed in a new thread
def task(lock):
    # acquire the lock
    print('Thread acquiring lock...')
    lock.acquire()
    # fail
    raise Exception('Something bad happened')
    # release the lock (never gets here)
    print('Thread releasing lock...')
    lock.release()

# create the mutex lock
lock = Lock()
# create and configure the new thread
thread = Thread(target=task, args=(lock,))
# start the new thread
thread.start()
# wait a while
sleep(1)
# acquire the lock
print('Main acquiring lock...')
lock.acquire()
# do something...
# release lock (never gets here)
lock.release()

运行该例子时,首先创建锁,然后创建并启动新的线程。然后主线程阻塞。新线程运行。它首先获得了锁,然后引发了一个异常而失败。该线程解开了锁,但却没有解开锁的代码。新的线程终止了。最后,主线程被唤醒,然后试图获取锁。由于锁没有被释放,主线程永远阻塞,导致了死锁。

Thread acquiring lock...
Exception in thread Thread-1:
Traceback (most recent call last):
  ...
Exception: Something bad happened
Main acquiring lock...

以上就是关于“怎么用Python模拟死锁”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

推荐阅读:
  1. Python爬虫怎么用Selenium模拟用户操作
  2. 达梦8 死锁模拟

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:uni-app开发之分包怎么配置

下一篇:uni-app商品分类页面怎么实现

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》