我有一个 API 监听传入的请求并将它们转储到列表中。在一个单独的过程中,我想在附加列表时触发某种操作。我尝试实例化一个新进程(来自多处理),但它不会在启动后更新数组的状态。
from multiprocessing import Process
import time
procs = []
def separateProcess(start, counter):
while True:
time.sleep(1)
print("length of the list from separate Process: "+str(len(procs)))
if __name__ == '__main__':
print("program started")
counter = 0
Process(target=separateProcess, args=(counter, counter)).start()
print("program end")
while True:
counter += 1
newstring = "string "+str(counter)
procs.append(newstring)
print("length of the list from main: " + str(len(procs)))
time.sleep(2)
这是输出:
length of the list from main: 1
length of the list from separate Process: 0
length of the list from main: 2
length of the list from separate Process: 0
length of the list from separate Process: 0
length of the list from main: 3
length of the list from separate Process: 0
length of the list from separate Process: 0
慕后森
相关分类