1

In my code I have two functions, the first function operate_camera updates the storage variable. The second function print_func prints the storage variable.

I would like to run these two functions concurrently, with the printing process happening with a delay of 15 seconds. However the operate_camera function contains a while loop. And by running the script, it will only run the process p2 once, and will stay stuck at the process p1.

For simplicity, I work with simple 1D arrays in the following code.

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()

The output will simply be stuck at

[0,0,0]

Are there any solutions within the multiprocessing package that can allow me to retain this form of code. If not, are there any alternative solutions to this problem?

Tian
  • 870
  • 3
  • 12
  • 24
  • Have you tried getting out a clock and waiting for a full minute? Also, thanks for the [mcve]! – wizzwizz4 Dec 28 '18 at 17:25
  • Since you're using multiprocessing you could have another `while` loop inside your print_func to constantly print your values and concurrently update them with `operate_camera` – Filip Młynarski Dec 28 '18 at 17:43

1 Answers1

1

First of all, do you really want multiprocessing here, instead of multithreading? You asked for alternative solutions, so, I come up with multithreading for this problem. You should check this answer for clarity, where mostly the same problem is discussed. So, I think the problem is that your print function executes only ones, hence you also want a loop there.

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()

You can notice, that this code uses global object which is not the best practice, but we need a shared object there.

In case of functions with parameters

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)
Kirill Korolev
  • 966
  • 3
  • 9
  • 22
  • Hi, thank you for the help. Will this work if I have another print function, i.e. `print_func1()`? – Tian Dec 28 '18 at 19:29
  • @Tian Yeah, create new thread as a camera_thread in the example above and combine it with the first example. Also, if this helps, you can mark the answer – Kirill Korolev Dec 29 '18 at 01:07
  • Okay, I would like to check as well. With two functions accessing the same store variable at different times, coupled with the fact that the `operate_camera function` is constantly updating the store variable, will there be any error that would arise?? Also, what is the use of `camera_thread.setDaemon(True)`? – Tian Dec 29 '18 at 14:10