1

I have two python scripts viz script_1.py and script_2.py This are small snippets of a project I am working. This is what I intend to do

  1. script_2 will start first, it will calculate the lenght of a list and will pass this count to script_1
  2. In script_1, it gets this count this will then generate a second list and if the newcount is greater than received count, it will pass a message stop to the script_2
  3. Finally, in script_2 there is a data function which has an infinite loop. Under that loop it will receive this message and if message is stop, it will break the loop and start over gain.

i have used Queue method of multiprocessing to achieve this. Below are the codes that i have, when i run them i do not get any results.

script_2.py

from multiprocessing import Process,Queue,Pipe
from script_1 import scan_fn

def data_fn(q2):
    msg = q2.get()
    print(msg)
    if(msg == 'stop'):  #this will be inside an infinite loop
        print('msg received, end the loop')

if __name__ == 'main':
    q1 = Queue()
    q2 = Queue()

    initial_list = [1,2,3,4,5,6,7,8]
    initalnumber = len(initial_list)
    q1.put(initalnumber)

    iscanprocess = Process(target=scan_fn,args=(q1,q2))
    rdataprocess = Process(target=data_fn,args=q2)

    iscanprocess.start()
    rdataprocess.start()

script_1.py

import multiprocessing
from multiprocessing import Pipe,Process,Queue

def scan_fn(q1,q2):
    ninitialdevice = q1.get()
    while True:
        new_list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
        newdevices = len(new_list)
        if (newdevices > ninitialdevice):
            q2.put('stop')

Please tell what is the correct way or what changes are needed.

1 Answer 1

1

Change script_2.py to the following code:

from multiprocessing import Process,Queue,Pipe
from script_1 import scan_fn

def data_fn(q2):
    msg = q2.get()
    print(msg)
    if(msg == 'stop'):  #this will be inside an infinite loop
        print('msg received, end the loop')

if __name__ == '__main__':
    q1 = Queue()
    q2 = Queue()

    initial_list = [1,2,3,4,5,6,7,8]
    initalnumber = len(initial_list)
    q1.put(initalnumber)

    iscanprocess = Process(target=scan_fn,args=(q1,q2))
    rdataprocess = Process(target=data_fn,args=(q2,))

    iscanprocess.start()
    rdataprocess.start()
Sign up to request clarification or add additional context in comments.

Comments

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.