<legend id='Wsie2'><style id='Wsie2'><dir id='Wsie2'><q id='Wsie2'></q></dir></style></legend>
  • <tfoot id='Wsie2'></tfoot>

      <i id='Wsie2'><tr id='Wsie2'><dt id='Wsie2'><q id='Wsie2'><span id='Wsie2'><b id='Wsie2'><form id='Wsie2'><ins id='Wsie2'></ins><ul id='Wsie2'></ul><sub id='Wsie2'></sub></form><legend id='Wsie2'></legend><bdo id='Wsie2'><pre id='Wsie2'><center id='Wsie2'></center></pre></bdo></b><th id='Wsie2'></th></span></q></dt></tr></i><div id='Wsie2'><tfoot id='Wsie2'></tfoot><dl id='Wsie2'><fieldset id='Wsie2'></fieldset></dl></div>

    1. <small id='Wsie2'></small><noframes id='Wsie2'>

        <bdo id='Wsie2'></bdo><ul id='Wsie2'></ul>
      1. Python多处理简单的方法来实现一个简单的计数器

        时间:2023-05-25

            <tbody id='j6wmr'></tbody>

          <small id='j6wmr'></small><noframes id='j6wmr'>

          <legend id='j6wmr'><style id='j6wmr'><dir id='j6wmr'><q id='j6wmr'></q></dir></style></legend>

              <bdo id='j6wmr'></bdo><ul id='j6wmr'></ul>

            • <i id='j6wmr'><tr id='j6wmr'><dt id='j6wmr'><q id='j6wmr'><span id='j6wmr'><b id='j6wmr'><form id='j6wmr'><ins id='j6wmr'></ins><ul id='j6wmr'></ul><sub id='j6wmr'></sub></form><legend id='j6wmr'></legend><bdo id='j6wmr'><pre id='j6wmr'><center id='j6wmr'></center></pre></bdo></b><th id='j6wmr'></th></span></q></dt></tr></i><div id='j6wmr'><tfoot id='j6wmr'></tfoot><dl id='j6wmr'><fieldset id='j6wmr'></fieldset></dl></div>

                • <tfoot id='j6wmr'></tfoot>
                  本文介绍了Python多处理简单的方法来实现一个简单的计数器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  大家好,我现在在 python 中使用多处理.我只是想知道是否存在某种简单的计数器变量,每个进程在完成处理某些任务时可以增加(有点像总共完成了多少工作).

                  Hey everyone, I am using multiprocessing in python now. and I am just wondering whether there exists some sort of simple counter variable that each process when they are done processing some task could just increment ( kind of like how much work done in total).

                  我查找了 Value 的 API,不认为它是可变的.

                  I looked up the API for Value, don't think it's mutable.

                  推荐答案

                  Value 确实是可变的;您从 ctypes 模块中指定所需的数据类型,然后可以对其进行变异.这是一个完整的工作脚本来演示这一点:

                  Value is indeed mutable; you specify the datatype you want from the ctypes module and then it can be mutated. Here's a complete, working script that demonstrates this:

                  from time import sleep
                  from ctypes import c_int
                  from multiprocessing import Value, Lock, Process
                  
                  counter = Value(c_int)  # defaults to 0
                  counter_lock = Lock()
                  def increment():
                      with counter_lock:
                          counter.value += 1
                  
                  def do_something():
                      print("I'm a separate process!")
                      increment()
                  
                  Process(target=do_something).start()
                  sleep(1)
                  print counter.value   # prints 1, because Value is shared and mutable
                  

                  Luper 在下面的评论中正确指出 Value 值默认被锁定.这是正确的,即使一个赋值由多个操作组成(例如赋值一个可能有很多字符的字符串),那么这个赋值也是原子的.但是,当递增计数器时,您仍然需要一个外部锁,如我的示例中提供的那样,因为递增加载当前值,然后递增它,然后将结果分配回 Value.

                  Luper correctly points out in a comment below that Value values are locked by default. This is correct in the sense that even if an assignment consists of multiple operations (such as assigning a string which might be many characters) then this assignment is atomic. However, when incrementing a counter you'll still need an external lock as provided in my example, because incrementing loads the current value and then increments it and then assigns the result back to the Value.

                  所以如果没有外部锁,你可能会遇到以下情况:

                  So without an external lock, you might run into the following circumstance:

                  • 进程 1(以原子方式)读取计数器的当前值,然后将其递增
                  • 在进程 1 可以将递增的计数器分配回 Value 之前,会发生上下文切换
                  • 进程 2 (原子地)读取计数器的当前(未递增)值,递增它,并将递增的结果(原子地)分配回 Value
                  • 进程 1 分配其增量值(原子地),消除进程 2 执行的增量
                  • Process 1 reads (atomically) the current value of the counter, then increments it
                  • before Process 1 can assign the incremented counter back to the Value, a context switch occurrs
                  • Process 2 reads (atomically) the current (unincremented) value of the counter, increments it, and assigns the incremented result (atomically) back to Value
                  • Process 1 assigns its incremented value (atomically), blowing away the increment performed by Process 2

                  这篇关于Python多处理简单的方法来实现一个简单的计数器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:python如何以分离模式运行进程 下一篇:如何限制多处理进程的范围?

                  相关文章

                  最新文章

                    <tfoot id='lSxch'></tfoot>

                  1. <i id='lSxch'><tr id='lSxch'><dt id='lSxch'><q id='lSxch'><span id='lSxch'><b id='lSxch'><form id='lSxch'><ins id='lSxch'></ins><ul id='lSxch'></ul><sub id='lSxch'></sub></form><legend id='lSxch'></legend><bdo id='lSxch'><pre id='lSxch'><center id='lSxch'></center></pre></bdo></b><th id='lSxch'></th></span></q></dt></tr></i><div id='lSxch'><tfoot id='lSxch'></tfoot><dl id='lSxch'><fieldset id='lSxch'></fieldset></dl></div>
                      <bdo id='lSxch'></bdo><ul id='lSxch'></ul>

                    <small id='lSxch'></small><noframes id='lSxch'>

                      <legend id='lSxch'><style id='lSxch'><dir id='lSxch'><q id='lSxch'></q></dir></style></legend>