包含对象方法的进程不识别对象的编辑

时间:2015-05-26 03:52:06

标签: python multiprocessing

我遇到以下情况 <?xml version="1.0" encoding="UTF-8"?> <modification> <id>customize orders page</id> <version>1.0</version> <vqmver>2.X</vqmver> <author>PaulR</author> <file name="admin/view/template/sale/order_list.tpl"> <operation info="add the card/delivery country text"> <search position="replace"><![CDATA[<td class="text-left"><?php echo $order['customer']; ?></td>]]></search> <add><![CDATA[ <td class="text-left"><?php echo $order['customer']; $tf_style1=($order['payment_country']==$order['shipping_country']?"font-size: x-small;":"font-size: x-small; color: red;"); echo "<br /><span style=\"".$tf_style1."\">".$order['payment_country']." / ".$order['shipping_country']."</span>"; ?></td> ]]></add> </operation> </file> </modification> 我想编辑sample_object的一个属性:process=Process(target=sample_object.run)

sample_object.edit_property(some_other_object)

_______________________________________________________

class sample_object:
       def __init__(self):
          self.storage=[]
       def edit_property(self,some_other_object):
          self.storage.append(some_other_object)
       def run:
          while True:
             if len(self.storage) is not 0:
                  print "1"
             #I know it's an infinite loop. It's just an example.

以前的代码永远不会打印&#34; 1&#34;。如何将Process连接到sample_object,以便进程对Process调用方法的对象进行编辑?换句话说,有没有办法让from multiprocessing import Process from sample import sample_object from sample2 import some_other_object class driver: if __name__ == "__main__": samp = sample_object() proc = Process(target=samp.run) proc.start() while True: some = some_other_object() samp.edit_property(some) #I know it's an infinite loop 能够识别.run中的变化?

谢谢。

1 个答案:

答案 0 :(得分:2)

您可以使用multiprocessing.Manager在进程之间共享Python数据结构。

from multiprocessing import Process, Manager


class A(object):

    def __init__(self, storage):
        self.storage = storage

    def add(self, item):
        self.storage.append(item)

    def run(self):
        while True:
            if self.storage:
                print 1


if __name__ == '__main__':

    manager = Manager()
    storage = manager.list()

    a = A(storage)

    p = Process(target=a.run)
    p.start()

    for i in range(10):
        a.add({'id': i})

    p.join()