【发布时间】:2019-12-10 20:51:15
【问题描述】:
扩展我之前提出的关于 python 中多进程之上的多线程的问题
Can I do multithreads on each process of a multiprocess program?
所以我正在尝试实现一个实现这一目标的示例。首先,我生成 2 个进程,每个进程将在其中创建 10 个线程,但这里看起来有些不对劲。我没有实现任何类型的锁或信号量,所以我希望输出会被打乱(如示例 3 所示)。当我运行此代码示例 1 和 2 时以正确的格式打印。例如 2 我什至尝试创建 2 个线程来启动每个进程,以确保它们不是按顺序启动的。 !为什么会这样?我错过了什么,协调发生在哪里?
import multiprocessing, threading, time
def startThreads(n):
threads = [threading.Thread(target=printer, args=(n, i)) for i in range(10)]
[t.start() for t in threads]
[t.join() for t in threads]
def printer(process_num, thread_num):
time.sleep(1)
print(f"Process number: {process_num} thread number: {thread_num}")
print(f"Process number: P thread number: T")
if __name__ == '__main__':
# Example 1
pros = [multiprocessing.Process(target=startThreads, args=(p_num, )) for p_num in range(5)]
[p.start() for p in pros]
[p.join() for p in pros]
# Process number: 0 thread number: 0
# Process number: P thread number: T
# Process number: 0 thread number: 4
# Process number: P thread number: T
# Process number: 0 thread number: 1
# Process number: P thread number: T
# Process number: 0 thread number: 2
# Process number: P thread number: T
# Process number: 0 thread number: 3
# Process number: P thread number: T
# Process number: 1 thread number: 0
# ...
# Example 2
print()
startThreads(0)
# Process number: 0 thread number: 1
# Process number: P thread number: TProcess number: 0 thread number: 0
# Process number: P thread number: T
# Process number: 0 thread number: 2Process number: 0 thread number: 4Process number: 0 thread number: 3
# Process number: P thread number: T
# Process number: P thread number: T
# Process number: P thread number: T
请注意示例 2 中的打印行为如何,另一方面,示例始终以正确的格式(安全打印)打印,而在这两种情况下,打印函数都由线程调用,当我删除打印时也会发生同样的事情格式化,而是使用要打印的固定字符串。
As the discussion in this question says 我们需要实现某种安全的打印方法来让每个打印语句都在一个新行中,但示例 1 不是这种情况
import multiprocessing, threading, time
def startThreads():
threads = [threading.Thread(target=printer) for i in range(5)]
[t.start() for t in threads]
[t.join() for t in threads]
def printer():
time.sleep(0.05)
print(f"Process number: P thread number: T")
if __name__ == '__main__':
p = multiprocessing.Process(target=startThreads)
p.start()
p.join()
print("=====================================")
startThreads()
# Process number: P thread number: T
# Process number: P thread number: T
# Process number: P thread number: T
# Process number: P thread number: T
# Process number: P thread number: T
# =====================================
# Process number: P thread number: TProcess number: P thread number: T
# Process number: P thread number: T
# Process number: P thread number: TProcess number: P thread number: T
#
我尝试只使用一个进程,但它仍然可以安全打印,其中每一行都在新行中打印,但是当我明确调用 startThreads 时,它的行为不一样并且不安全打印,是它的行为是这样的?!
【问题讨论】:
-
您使用的是哪个 python 版本(影响 GIL)?哪个操作系统(影响抢占式调度)?
-
无论如何,在我的机器(linux mint 19 和 python 3.8.1)上,输出在两种情况下都不同步。如果你总是得到相同的结果,它必须以某种方式依赖于 os/python 版本。
标签: python python-3.x multithreading multiprocessing