2016-12-06 90 views
0

今天我写了这篇文章Multiprocessing Error with Results无结构多处理打印命令

现在我修改这个脚本:

import multiprocessing 

class class1(): 
    def classfunction1(self, a): 
     self.x = a 
     print("class 1") 


class class2(): 
    def classfunction2(self, a): 
     self.y = a 
     print("class 2") 

def test(i): 
    print("I'm in the Testfunction") 
    b = i * class1.x * class2.y 

    return b 

def init(): # added 
    print("I'm in the Initfunction") 
    global class1, class2 
    class1 = class1() 
    class2 = class2() 
    x = 1 
    y = 2 
    class1.classfunction1(x) 
    class2.classfunction2(y) 

if __name__ == "__main__": 
    init() # explicit call here 
    print("This variable is callable", class1.x) 
    print("And this one is also callable", class2.y) 
    counter = [] 
    for i in range(10): 
     counter.append(i) 
    pool = multiprocessing.Pool(initializer=init, processes=4) # implicit call 
    results = pool.imap(test, counter) 
    pool.close() 
    pool.join() 
    resultslist = list(results) 
    print(resultslist) 

我插入一些打印命令的类。但结果是无结构的印刷,如:

I'm in the Initfunction 
class 1 
class 2 
This variable is callable 1 
And this one is also callable 2 
I'm in the Initfunction 
class 1 
class 2 
I'm in the Testfunction 
I'm in the Testfunction 
I'm in the Testfunction 
I'm in the Testfunction 
I'm in the Testfunction 
I'm in the Testfunction 
I'm in the Testfunction 
I'm in the Testfunction 
I'm in the Testfunction 
I'm in the Testfunction 
I'm in the Initfunction 
class 1 
class 2 
I'm in the Initfunction 
class 1 
class 2 
I'm in the Initfunction 
class 1 
class 2 
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18] 

在我只想一旦类印刷品......只有文本“我在Testfunction”我要多(10次)。

有没有人可能的解决方案?

+0

你能举个例子吗?我不明白你是怎么意思的...... – John28

回答

1

正如我(很差)试图评论(现已删除)来解释,你可以用默认值添加一个可选的参数为init()功能(以及类的方法)做到这一点:

from __future__ import print_function 
import multiprocessing 
import sys 

sys_print = print # save built-in print function before replacement is defined 

def print(*args, **kwargs): 
    """Replacement for built-in that flushes output stream after each call.""" 
    sys_print(*args, **kwargs) 
    stdout = kwargs.get('file', sys.stdout) 
    stdout.flush() # force any buffered output to be displayed 

class class1(): 
    # note addition of optional argument with default value 
    def classfunction1(self, a, notify=False): 
     self.x = a 
     if notify: print("class 1") 

class class2(): 
    # note addition of optional argument with default value 
    def classfunction2(self, a, notify=False): 
     self.y = a 
     if notify: print("class 2") 

def test(i): 
    print("I'm in the Testfunction") 
    b = i * class1.x * class2.y 

    return b 

def init(notify=False): # note addition of optional argument with default value 
    if notify: print("I'm in the Initfunction") 
    global class1, class2 
    class1 = class1() 
    class2 = class2() 
    x = 1 
    y = 2 
    class1.classfunction1(x, notify) 
    class2.classfunction2(y, notify) 

if __name__ == "__main__": 
    init(True) # override default arg in this explicit call 
    print("This variable is callable", class1.x) 
    print("And this one is also callable", class2.y) 
    counter = list(range(10)) 
    pool = multiprocessing.Pool(initializer=init, processes=4) # implicit calls 
    results = pool.imap(test, counter) 
    pool.close() 
    pool.join() 
    resultslist = list(results) 
    print(resultslist) 

输出:

I'm in the Initfunction 
class 1 
class 2 
This variable is callable 1 
And this one is also callable 2 
I'm in the Testfunction 
I'm in the Testfunction 
I'm in the Testfunction 
I'm in the Testfunction 
I'm in the Testfunction 
I'm in the Testfunction 
I'm in the Testfunction 
I'm in the Testfunction 
I'm in the Testfunction 
I'm in the Testfunction 
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18] 
+0

供参考:另一种让你想要的类“可调用”的方法是通过定义['__call__()'](https://docs.python.org/3 /reference/datamodel.html#object.__call__)方法。这比使用它自己的实例替换类更为可取(当前使用的'class1 = class1()'技巧)。 – martineau

+0

@martinuea:我不明白为什么文本“这个变量是可调用的”和“这个也是可调用的”在Testfunction-print和class print后面打印?该命令在多处理开始之前。在Testfunction-print之前需要更改哪些class1和class2?我期望的顺序是“This variable ..”和“This this ...”,然后是“Initfunction”,“class1”,“class2”,“Testfunction”,并且至少包含变量结果中的数字... – John28

+0

输出的奇怪排序是因为它来自几个不同的进程并发运行,这导致一些尝试同时将东西打印到同一共享控制台。我很惊讶这不是更多的混杂... – martineau