如何使用多处理 python 更新和检索图像?
How do I update and retrieve an image using multiprocessing python?
在我的代码中有两个函数,第一个函数 operate_camera
更新存储变量。第二个函数 print_func
打印存储变量。
我想运行同时执行这两个功能,打印过程延迟 15 秒。但是 operate_camera
函数包含一个 while
循环。通过 运行ning 脚本,它只会 运行 进程 p2
一次,并且会停留在进程 p1
.
为简单起见,我在以下代码中使用了简单的一维数组。
from multiprocessing import Process, Array
import numpy as np
import time
def operate_camera(store):
while True: # loop that updates image
store = store + np.ones_like(store)
def print_func(store):
print(store)
time.sleep(15)
if __name__ == "__main__":
store = np.array([0,0,0])
p1 = Process(target=operate_camera, args=(store,))
p2 = Process(target=print_func, args=(store,))
p1.start()
p2.start()
输出将简单地停留在
[0,0,0]
多处理包中是否有任何解决方案可以让我保留这种形式的代码。如果没有,是否有其他解决方案可以解决此问题?
首先,你真的想要多处理,而不是多线程吗?
你要求替代解决方案,所以,我想出了多线程来解决这个问题。为了清楚起见,您应该检查 this answer,其中主要讨论了相同的问题。所以,我认为问题是你的打印函数只执行一个,因此你也想要一个循环。
from threading import Thread
import numpy as np
import time
store = np.array([0, 0, 0])
def operate_camera():
while True:
global store
store += np.ones_like(store)
def print_func():
while True:
time.sleep(15)
print(store)
if __name__ == "__main__":
t1 = Thread(target=operate_camera)
t2 = Thread(target=print_func)
t1.start()
t2.start()
# other code
t1.join()
t2.join()
你可以注意到,这段代码使用了全局对象,这不是最佳实践,但我们需要一个共享对象。
对于带参数的函数
from threading import Thread
import numpy as np
import time
def operate_camera(store):
while True:
store += np.ones_like(store)
def print_func(store):
time.sleep(1)
print(store)
if __name__ == "__main__":
store = np.array([0, 0, 0])
camera_thread = Thread(target=operate_camera, args=(store, ))
camera_thread.setDaemon(True)
camera_thread.start()
while True:
print_func(store)
在我的代码中有两个函数,第一个函数 operate_camera
更新存储变量。第二个函数 print_func
打印存储变量。
我想运行同时执行这两个功能,打印过程延迟 15 秒。但是 operate_camera
函数包含一个 while
循环。通过 运行ning 脚本,它只会 运行 进程 p2
一次,并且会停留在进程 p1
.
为简单起见,我在以下代码中使用了简单的一维数组。
from multiprocessing import Process, Array
import numpy as np
import time
def operate_camera(store):
while True: # loop that updates image
store = store + np.ones_like(store)
def print_func(store):
print(store)
time.sleep(15)
if __name__ == "__main__":
store = np.array([0,0,0])
p1 = Process(target=operate_camera, args=(store,))
p2 = Process(target=print_func, args=(store,))
p1.start()
p2.start()
输出将简单地停留在
[0,0,0]
多处理包中是否有任何解决方案可以让我保留这种形式的代码。如果没有,是否有其他解决方案可以解决此问题?
首先,你真的想要多处理,而不是多线程吗? 你要求替代解决方案,所以,我想出了多线程来解决这个问题。为了清楚起见,您应该检查 this answer,其中主要讨论了相同的问题。所以,我认为问题是你的打印函数只执行一个,因此你也想要一个循环。
from threading import Thread
import numpy as np
import time
store = np.array([0, 0, 0])
def operate_camera():
while True:
global store
store += np.ones_like(store)
def print_func():
while True:
time.sleep(15)
print(store)
if __name__ == "__main__":
t1 = Thread(target=operate_camera)
t2 = Thread(target=print_func)
t1.start()
t2.start()
# other code
t1.join()
t2.join()
你可以注意到,这段代码使用了全局对象,这不是最佳实践,但我们需要一个共享对象。
对于带参数的函数
from threading import Thread
import numpy as np
import time
def operate_camera(store):
while True:
store += np.ones_like(store)
def print_func(store):
time.sleep(1)
print(store)
if __name__ == "__main__":
store = np.array([0, 0, 0])
camera_thread = Thread(target=operate_camera, args=(store, ))
camera_thread.setDaemon(True)
camera_thread.start()
while True:
print_func(store)