0

I have two functions and needed the return values to proceed with the further part of the script...but currently my code giving only the output of the first function...

import multiprocessing
def gm(name):
    h = "Good Morning"+str(name)
    qout.put(h)
def sal(name):
    k="Hi "+str(name)
    qout.put(k)
if __name__ == '__main__':
    qout = multiprocessing.Queue()
    p1 = multiprocessing.Process(target=gm, args=("ashin",))
    p2 = multiprocessing.Process(target=sal, args=("ashin",))
    p1.start()
    p2.start()
    p1.join()
    p2.join()
result = qout.get()

#output - "Good Morning ashin"

#required output - "Good Morning ashin" & "Hi ashin"

Appreciate your help......

2
  • 1
    well you need a bit of refactoring: you need to add the que to the Processing call as an argument, then you need to call gue.get in the beginning of each function, as after the "calculation" use put to populate the que. Lastly, in the main you need to read out the que Commented Dec 7, 2021 at 13:36
  • 1
    Try result = []; while not qout.empty():; result.append(qout.get()) Commented Dec 7, 2021 at 14:18

2 Answers 2

1

qout.get() gets you the first element from queue. I do not know the bigger picture of what you're are trying to achieve, but you can get all elements from queue like in the following.

from multiprocessing import Process, Queue

def gm(name):
    h = "Good Morning"+str(name)
    qout.put(h)

def sal(name):
    k="Hi "+str(name)
    qout.put(k)

if __name__ == '__main__':
    qout = Queue()
    p1 = Process(target=gm, args=("ashin",))
    p2 = Process(target=sal, args=("ashin",))
    p1.start()
    p2.start()
    p1.join()
    p2.join()

    list1 = []
    while not qout.empty():
        list1.append(qout.get())
    temp = list(map(str, list1))
print(" & ".join(temp))

output

Hi ashin & Good Morningashin
Sign up to request clarification or add additional context in comments.

3 Comments

Now its working.....I am thankful to you all helping me..I also have a small doubt about the ordering in the qout.get(),like will it be the order of the jobs finished by each processes or in the given order of processes like p1, p2...if it is in the order of completion then i also like to know can we order the result list in the order of the processes like list1 = [p1, p2]....because i need to assign the values into different variables and dont be miss assigned the [email protected] @Timus @ Vijayendar Gururaja
@Ashin, During multi threading or multiprocessing, the order of execution or completion completely depends on the python interpreter's internal implementation. You should not depend on the multiprocessing since it would not guarantee the expected result order.
Thats right.....but i got another way to deal with this.By adding priority key in qout.put("1", output1).......thanks @Ramachandran Rajasekaran
1

Instead of managing your own output queue, just use the latest Python 3 concurrency features:

from concurrent.futures import as_completed, ProcessPoolExecutor

def gm(name):
    return f'Good Morning {name}'

def sal(name):
    return f'Hi {name}'

if __name__ == '__main__':
    with ProcessPoolExecutor() as exe:
        futures = [exe.submit(x, 'ashin') for x in (gm, sal)]
    for future in as_completed(futures):
        print(future.result())

1 Comment

Good to know...Thanks @Velimir Mlaker

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.