Django中的线程同步 [英] Thread Synchronization in Django

查看:68
本文介绍了Django中的线程同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以像在Django中那样阻止Java阻塞关键区域吗?

Is there any way to block a critical area like with Java synchronized in Django?

推荐答案

您可以使用锁来使确保一次只有一个线程可以访问某个代码块。

You can use locks to make sure that only one Thread will access a certain block of code at a time.

为此,您只需创建一个Lock对象,然后在代码块之前获取锁即可。您要同步。例如:

To do this, you simply create a Lock object then acquire the lock before the block of code you want to synchronize. An example:

lock = Lock()

lock.acquire()   # will block if another thread has lock
try:
    ... use lock
finally:
    lock.release() 

有关更多信息,请参见 http:// effbot。 org / zone / thread-synchronization.htm

For more information , see http://effbot.org/zone/thread-synchronization.htm.

这篇关于Django中的线程同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆