0
#Each pic's size is 1280x720 
import time

from multiprocessing import Process,JoinableQueue,Queue,Value

**def create_row(M, q_row,y_val):**    #creating lines. len:720 len of each     

    line_len=720
    col_len=1280

    for i in range(line_len):
       q_row.put(M[(i*col_len):((i+1)*col_len)])


**def create_col(M, q_col,x_val):** #creating cols

    col_len=1280  
    line_len=720

    for i in range(col_len): 
        l = list() 
        for j in range(line_len):
            l.append(M[j*line_len + i])    
        q_col.put(l)

**def main():**

    #creating row and col Queue
    q_row=JoinableQueue()
    q_col=JoinableQueue()

    x_val=Value('i',-1)
    y_val=Value('i',-1)

    p1=Process(target=create_row,args=(M,q_row,y_val))
    p2=Process(target=create_col,args=(M,q_col,x_val))
    q_row.join()
    q_col.join()
    p1.start()
    p2.start()
    p1.join()
    p2.join()

Now to the problem:
M is a list in size of 1280*720, which represent a picture.
We want to divide the list into rows and cols, so we can go through the picture faster.
We tried few things:

  1. Without joining the processes: in that scenario only one of the functions will work (We will get either the row_q only or the col_q)

  2. With joining the process:: The program will freeze.

What should I do?

Dave2e
  • 22,192
  • 18
  • 42
  • 50
ItsMe
  • 1
  • 2

1 Answers1

0

A similar question has been answered here,

Here's something from the docs.

Warning

As mentioned above, if a child process has put items on a queue (and it has not used JoinableQueue.cancel_join_thread), then that process will not terminate until all buffered items have been flushed to the pipe.

This means that if you try joining that process you may get a deadlock unless you are sure that all items which have been put on the queue have been consumed. Similarly, if the child process is non-daemonic then the parent process may hang on exit when it tries to join all its non-daemonic children.

Note that a queue created using a manager does not have this issue.

With the queue created with multiprocessing.Manager, this issues is resolved.

import time
from multiprocessing import Process,JoinableQueue,Queue,Value
import multiprocessing
def create_row( q_row,y_val): #creating lines. len:720 len of each
    line_len=720

    for i in range(line_len):
        q_row.put(1)

def create_col(q_col,x_val): #creating cols
    col_len=1280
    line_len=720
    for i in range(col_len):
        l = list()
        for j in range(line_len):
            l.append(1)
        q_col.put(l)

def main():
    #creating row and col Queue
    manager = multiprocessing.Manager()

    q_row=manager.JoinableQueue()
    q_col=manager.JoinableQueue()

    x_val=Value('i',-1)
    y_val=Value('i',-1)

    p1=Process(target=create_row,args=(q_row,y_val))
    p2=Process(target=create_col,args=(q_col,x_val))

    q_row.join()
    q_col.join()

    p1.start()
    p2.start()


    p1.join()
    p2.join()
main()
Aboorva Devarajan
  • 1,517
  • 10
  • 23