如何在python中使用信号量?

2024-09-27 21:22:50 发布

您现在位置:Python中文网/ 问答频道 /正文

为了访问数据库并避免同时进行两次提交,我尝试使用BoundedSemaphoresee here),如下所示:

from threading import BoundedSemaphore
...
commit_sema = BoundedSemaphore(value=1) 

class Sqlite(object):
    ...
    def commit():
        commit_sema.acquire()
        self.conn.commit()  # self.conn is the Connection object
        commit_sema.release()

但是,尽管我确保使用了Sqlite类的同一个实例,但我发现在第一个调用完成之前,第二个commit()调用已经发出。在

我的印象是第二个调用应该被阻止,直到第一个调用release()来增加Semaphore计数器。但事实并非如此。在

那么我觉得这有什么不对?如何正确操作?在


Tags: fromimportself数据库sqlitereleasehereobject

热门问题