Mar*_*aky 6 python multithreading multiprocessing python-3.x
扩展我之前问过的关于 python 中多进程之上的多线程的问题
所以我正在尝试实现一个实现这一目标的示例。首先,我生成了 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
Run Code Online (Sandbox Code Playgroud)
请注意示例二中的打印行为如何,另一方面,示例始终以正确的格式(安全打印)打印,而在这两种情况下,打印函数都由线程调用,当我删除打印格式时会发生同样的事情,而是使用要打印的固定字符串。
正如这个问题中的讨论所说,我们需要实现某种安全的打印方法来在新行中获取每个打印语句,但示例 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
#
Run Code Online (Sandbox Code Playgroud)
我尝试只使用一个进程,但它仍然安全打印,其中每一行都在新行中打印,但是当我startThreads
明确调用时,它的行为不一样并且不安全打印,它的行为是这样的吗?!
使用相同的代码我得到scrambled
输出:
1:20:21:3, , 0:00:1, 0:71:5, , 1:40:91:1, 1:6, , , 1:0, , 0:5, 0:4, 0:3, 0:6, 1:7, 0:8, 1:9, , 1:8,
0:0, 0:10:2, , 0:3, 0:4, 0:50:6, 1:0, , 1:1, 0:7, 1:2, 1:3, 0:9, 0:81:5, 1:4, , 1:71:6, , 1:91:8, ,
0:0, 0:1, 0:40:3, 0:2, , 0:60:5, , 0:70:8, , 0:9,
Run Code Online (Sandbox Code Playgroud)
尝试运行多次。如果 1 和 2 总是被扰乱 - 也许它依赖于平台。
所以我希望输出被打乱
它不以任何方式同步。顺序是随机的:)
归档时间: |
|
查看次数: |
244 次 |
最近记录: |