gpt4 book ai didi

Python多进程输出同一行

转载 作者:行者123 更新时间:2023-12-01 02:17:28 26 4
gpt4 key购买 nike

我这里有一个简单的脚本:

from multiprocessing import Process

def one():
x = 1
while x < 100:
x +=1
print(x)


def two():
y = 100
while y < 200:
y += 1
print(y)


if __name__=='__main__':
p1 = Process(target = one)
p1.start()
p2 = Process(target = two)
p2.start()

它工作正常并打印出数字列表。

我一直在寻找,但到目前为止还无法在这里找到任何答案。我想知道是否可以在同一行上打印这样的 def 1 和 2 返回的结果,以便我可以比较输出。像这样的东西:

1, 100
2, 101
3, 102
4, 103
...
...
100, 200

也许我错过了一些明显的东西,或者甚至是不可能的。

最佳答案

也许类似这样但使用生成器:

def one():
x = 1
while x < 100:
yield x
x +=1


def two():
y = 100
while y < 200:
yield y
y += 1


if __name__=='__main__':
p1 = one()
p2 = two()

for x, y in zip(p1, p2):
print x, y

关于Python多进程输出同一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48271576/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com