python 中的多线程同步

发布于 2024-07-22 12:57:45 字数 608 浏览 4 评论 0原文

我遇到一个问题,我需要 x 个线程等待它们全部到达同步点。 我的解决方案使用下面的 synchronise 方法,每个线程函数在需要同步时都会调用该方法。

有一个更好的方法吗?

thread_count = 0
semaphore = threading.Semaphore()
event = threading.Event()

def synchronise(count):
    """ All calls to this method will block until the last (count) call is made """
    with semaphore:
        thread_count += 1
        if thread_count == count:
            event.set()

    event.wait()

def threaded_function():
    # Do something

    # Block until 4 threads have reached this point
    synchronise(4)

    # Continue doing something else

I have a problem where I need x threads to wait until they have all reached a synchronization point. My solution uses the synchronise method below which is called by each threaded function when they need to synchronise.

Is there a better way to do this?

thread_count = 0
semaphore = threading.Semaphore()
event = threading.Event()

def synchronise(count):
    """ All calls to this method will block until the last (count) call is made """
    with semaphore:
        thread_count += 1
        if thread_count == count:
            event.set()

    event.wait()

def threaded_function():
    # Do something

    # Block until 4 threads have reached this point
    synchronise(4)

    # Continue doing something else

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蔚蓝源自深海 2024-07-29 12:57:45

请注意,Barrier 已从 Python 3.2 起实现

使用屏障的示例:

from threading import Barrier, Thread

def get_votes(site):
    ballots = conduct_election(site)
    all_polls_closed.wait()        # do not count until all polls are closed
    totals = summarize(ballots)
    publish(site, totals)

all_polls_closed = Barrier(len(sites))
for site in sites:
    Thread(target=get_votes, args=(site,)).start()

Note that Barrier has been implemented as of Python 3.2

Example of using barriers:

from threading import Barrier, Thread

def get_votes(site):
    ballots = conduct_election(site)
    all_polls_closed.wait()        # do not count until all polls are closed
    totals = summarize(ballots)
    publish(site, totals)

all_polls_closed = Barrier(len(sites))
for site in sites:
    Thread(target=get_votes, args=(site,)).start()
稚然 2024-07-29 12:57:45

同步线程的方法有很多种。 许多。

除了同步之外,您还可以执行以下操作。

  1. 围绕同步点将您的任务分成两个步骤。 启动线程执行预同步步骤。 然后使用“join”等待所有线程完成步骤 1。启动新线程执行同步后步骤。 与同步相比,我更喜欢这个。

  2. 创建队列; 获取同步锁。 启动所有线程。 每个线程都会在队列中放入一个条目并等待同步锁。 “主”线程位于循环中,使项目从队列中出列。 当所有线程都将一个项目放入队列时,“主”线程释放同步锁。 所有其他线程现在可以再次自由运行。

有多种进程间通信(IPC)技术——所有这些技术都可以用于线程同步。

There are many ways to synchronize threads. Many.

In addition to synchronize, you can do things like the following.

  1. Break your tasks into two steps around the synchronization point. Start threads doing the pre-sync step. Then use "join" to wait until all threads finish step 1. Start new threads doing the post-sync step. I prefer this, over synchronize.

  2. Create a queue; acquire a synchronization lock. Start all threads. Each thread puts an entry in the queue and waits on the synchronization lock. The "main" thread sits in a loop dequeueing items from the queue. When all threads have put an item in the queue, the "main" thread releases the synchronization lock. All other threads are now free to run again.

There are a number of interprocess communication (IPC) techniques -- all of which can be used for thread synchronization.

看轻我的陪伴 2024-07-29 12:57:45

您想要的功能称为“屏障”。 (不幸的是,在谈论线程时,该术语有两种含义。因此,如果您 Google 即可,忽略谈论“内存屏障" - 这是一个非常不同的事情)。

你的代码看起来很合理——简单又安全。

我找不到任何 Python 屏障的“标准”实现,因此我建议您继续使用您的代码。

The functionality you want is called a "barrier". (Unfortunately that term has 2 meanings when talking about threading. So if you Google it, just ignore articles that talk about "memory barriers" - that's a very different thing).

Your code looks quite reasonable - it's simple and safe.

I couldn't find any "standard" implementations of barriers for Python, so I suggest you keep using your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文