没有结构的多处理打印命令

Multiprocessing print command without structure

今天写了这个post.

现在我将这个脚本修改为:

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]

类中的打印件我只想要一次...只有文字"I'm in the Testfunction"我想要多次(10次)。

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

正如我(糟糕地)试图在评论(现已删除)中解释的那样,您可以通过向 init() 函数(以及 class 方法):

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]