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

    <tfoot id='k45U8'></tfoot>

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

        <legend id='k45U8'><style id='k45U8'><dir id='k45U8'><q id='k45U8'></q></dir></style></legend>
      1. 如何在 Python 中使用 Managers() 在多个进程之间共享

        时间:2023-05-26
          <bdo id='MeHtj'></bdo><ul id='MeHtj'></ul>
        • <legend id='MeHtj'><style id='MeHtj'><dir id='MeHtj'><q id='MeHtj'></q></dir></style></legend>

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

                  <tbody id='MeHtj'></tbody>

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

                  <tfoot id='MeHtj'></tfoot>

                  本文介绍了如何在 Python 中使用 Managers() 在多个进程之间共享字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我需要从主进程中读取由 multiprocessing.Process 实例编写的字符串.我已经使用管理器和队列将参数传递给进程,所以使用管理器似乎很明显,但是Managers不支持字符串:

                  I need to read strings written by multiprocessing.Process instances from the main process. I already use Managers and queues to pass arguments to processes, so using the Managers seems obvious, but Managers do not support strings:

                  Manager() 返回的管理器将支持类型列表、字典、命名空间、锁、RLock、信号量、有界信号量、条件、事件、队列、值和数组.

                  A manager returned by Manager() will support types list, dict, Namespace, Lock, RLock, Semaphore, BoundedSemaphore, Condition, Event, Queue, Value and Array.

                  如何使用多处理模块中的管理器共享由字符串表示的状态?

                  How do I share state represented by a string using Managers from the multiprocessing module?

                  推荐答案

                  multiprocessing的Managers可以持有Values 又可以包含 c_char_p 来自 ctypes 模块:

                  multiprocessing's Managers can hold Values which in turn can hold instances of the type c_char_p from the ctypes module:

                  >>> import multiprocessing
                  >>> import ctypes
                  >>> v = multiprocessing.Value('c', "Hello, World!")
                  Traceback (most recent call last):
                    File "<stdin>", line 1, in <module>
                    File "/usr/lib/python2.7/multiprocessing/__init__.py", line 253, in Value
                      return Value(typecode_or_type, *args, **kwds)
                    File "/usr/lib/python2.7/multiprocessing/sharedctypes.py", line 99, in Value
                      obj = RawValue(typecode_or_type, *args)
                    File "/usr/lib/python2.7/multiprocessing/sharedctypes.py", line 73, in RawValue
                      obj.__init__(*args)
                  TypeError: one character string expected
                  >>> cstring = multiprocessing.Value(ctypes.c_char_p, "Hello, World!")
                  >>> cstring
                  <Synchronized wrapper for c_char_p(166841564)>
                  >>> cstring.value
                  'Hello, World!'
                  

                  对于 Python 3, 使用 c_wchar_p 代替 c_char_p

                  For Python 3, use c_wchar_p instead of c_char_p

                  另请参阅:发布我很难找到的原始解决方案.

                  因此,可以使用 Manager 在 Python 中的多个进程下共享字符串,如下所示:

                  So a Manager can be used to share a string beneath multiple processes in Python like this:

                  >>> from multiprocessing import Process, Manager, Value
                  >>> from ctypes import c_char_p
                  >>> 
                  >>> def greet(string):
                  >>>     string.value = string.value + ", World!"
                  >>> 
                  >>> if __name__ == '__main__':
                  >>>     manager = Manager()
                  >>>     string = manager.Value(c_char_p, "Hello")
                  >>>     process = Process(target=greet, args=(string,))
                  >>>     process.start()
                  >>>     process.join()    
                  >>>     print string.value
                  'Hello, World!'
                  

                  这篇关于如何在 Python 中使用 Managers() 在多个进程之间共享字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何杀死多进程中的所有池工作人员? 下一篇:Python Multiprocessing - 将类方法应用于对象列表

                  相关文章

                  最新文章

                • <small id='hSHHD'></small><noframes id='hSHHD'>

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

                      <legend id='hSHHD'><style id='hSHHD'><dir id='hSHHD'><q id='hSHHD'></q></dir></style></legend>
                        <bdo id='hSHHD'></bdo><ul id='hSHHD'></ul>