in Education by
I need to react in a main process to random events happening in a child process. I have implemented this with a queue between the main and the child process, and a 'queue poller' running in a secondary thread of the main process and calling a callback function each time it finds an item in the queue. The code is below and seems to work. Question 1: Could you please tell me if the strategy is correct or if something simpler exists ? Question 2: I tried to have both the child process and the secondary thread terminated when stopping the main loop, but it fails, at least in spyder. What should I do to terminate everything properly? Thanks for your help :-) from threading import Thread from multiprocessing import Process, Queue from time import sleep from random import random class MyChildProcess(Process): """ This process runs as a child process of the main process. It fills a queue (instantiated in the main process - main thread) at random times. """ def __init__(self,queue): super(MyChildProcess,self).__init__() self._q = queue # memorizes the queue self._i = 0 # attribute to be incremented and put in the queue def run(self): while True: self._q.put(self._i) # puts in the queue self._i += 1 # increment for next time sleep(random()) # wait between 0 and 1s class myListenerInSeparateThreadOfMainProcess(): """ This listener runs in a secondary thread of the main process. It polls a queue and calls back a function for each item found. """ def __init__(self, queue, callbackFunction): self._q = queue # memorizes the queue self._cbf = callbackFunction # memorizes the queue self.pollQueue() def pollQueue(self): while True: sleep(0.2) # polls 5 times a second max self.readQueue() def readQueue(self): while not self._q.empty(): # empties the queue each time self._cbf(self._q.get()) # calls the callback function for each item def runListener(q,cbf): """Target function for the secondary thread""" myListenerInSeparateThreadOfMainProcess(q,cbf) def callBackFunc(*args): """This is my reacting function""" print 'Main process gets data from queue: ', args if __name__ == '__main__': q= Queue() t = Thread(target=runListener, args=(q,callBackFunc)) t.daemon=True # try to have the secondary thread terminated if main thread terminates t.start() p = MyChildProcess(q) p.daemon = True # try to have the child process terminated if parent process terminates p.start() # no target scheme and no parent blocking by join while True: # this is the main application loop sleep(2) print 'In main loop doing something independant from the rest' Here is what I get: Main process gets data from queue: (0,) Main process gets data from queue: (1,) Main process gets data from queue: (2,) Main process gets data from queue: (3,) In main loop doing something independant from queue management Main process gets data from queue: (4,) Main process gets data from queue: (5,) Main process gets data from queue: (6,) Main process gets data from queue: (7,) In main loop doing something independant from queue management Main process gets data from queue: (8,) Main process gets data from queue: (9,) In main loop doing something independant from queue management ... JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

Related questions

0 votes
    Hey there Stack Overflow. I'm trying to build a testing script that should mix outputting changing ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    If I use socket.makefile and then close the file object as well as the underlying socket, then subsequent ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    In the next examlpe I have Socket server handled in the thread. In the handle function we create Thread ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    In the next examlpe I have Socket server handled in the thread. In the handle function we create Thread ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    In the next examlpe I have Socket server handled in the thread. In the handle function we create Thread ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have an endless loop problem in Simpy simulation. My scenario is: After finishing triage, a patient wait ... ): admin_decision_prob = random.uniform(0, 1) if admin_decision_prob...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    Assume that in a family, each child is equally likely to be a boy or a girl. A family with three children is chosen at ... . 2/3 D. 4/7 Select the correct answer from above options...
asked Nov 20, 2021 in Education by JackTerrance
0 votes
    Write two points difference between Null loop and infinite loop Select the correct answer from above options...
asked Dec 17, 2021 in Education by JackTerrance
0 votes
    When new SaveModelAction() is called, the corresponding Effect gets stuck in an infinite loop. This the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    When new SaveModelAction() is called, the corresponding Effect gets stuck in an infinite loop. This the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I have to make this an infinite loop and also make it go back to the start when a wrong character ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I was reading the great article about binding and unbinding events (because I am a js beginner using jQuery ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 9, 2022 in Education by JackTerrance
0 votes
    I was reading the great article about binding and unbinding events (because I am a js beginner using jQuery ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 8, 2022 in Education by JackTerrance
0 votes
    I was reading the great article about binding and unbinding events (because I am a js beginner using jQuery ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 8, 2022 in Education by JackTerrance
...