multithreading - Toggle a loop on and off in a multi-processed program in Python -
i running simulation program in python requires me run large number of processes. each of these processes has loop executes block of code.
the problem facing need processes run unknown amount of time (until experiment complete). there no way know value of time beforehand.
i looking way terminate processes or have them stop executing when experiment complete.
currently, have created flag variable using manager in python's multiprocessing module. each of child processes contain loop executes while flag set true. solves problem generates many errors, 1 each process, when toggle flag set false.
traceback (most recent call last): file "c:\python33\lib\multiprocessing\process.py", line 258, in _bootstrap self.run() file "c:\python33\lib\multiprocessing\process.py", line 95, in run self._target(*self._args, **self._kwargs) file "c:\users\michael\pycharmprojects\untitled1\pmonitor.py", line 33, in backgroundtasklauncher while flag[0]: file "<string>", line 2, in __getitem__ file "c:\python33\lib\multiprocessing\managers.py", line 726, in _callmethod conn.send((self._id, methodname, args, kwds)) file "c:\python33\lib\multiprocessing\connection.py", line 207, in send self._send_bytes(buf.getbuffer()) file "c:\python33\lib\multiprocessing\connection.py", line 281, in _send_bytes ov, err = _winapi.writefile(self._handle, buf, overlapped=true) brokenpipeerror: [winerror 232] pipe being closed process process-20:
i wondering if there proper way trying go here. i'm not sure if i'm using correct terminology.
the code main process follows:
if __name__ == '__main__': manager = manager() flag = manager.list([true]) tasksize in tasksizes: flag[0] = true in range(1,task_launchers): process(target=backgroundtasklauncher, args=(tasksize,flag)).start() #experiment goes here flag[0] = false
the code process launched is:
def backgroundtasklauncher(tasksize,flag): while flag[0]: in range(0,chunk_amount): thread(target=task, args=(tasksize,)).start() sleep(microchunk_gap) sleep(interchunk_gap*random.random()*2)
essentially main method calls number of backgroundtasklauncher processes who, in turn, launch many threads while toggle flag enabled , stop , complete when flag disabled.
i looking proper way go getting behavior.
i think missing join child processes before program ends. child processes left without daddy ;-)
try this:
if __name__ == '__main__': manager = manager() flag = manager.list([true]) tasksize in tasksizes: flag[0] = true processes = [] # list store process handles in range(1,task_launchers): p = process(target=backgroundtasklauncher, args=(tasksize,flag)) p.start() processes.append(p) # save process handle # experiment goes here (i think goes here (unindented(?))) flag[0] = false # after done experiment, join child processes p in processes: p.join()
Comments
Post a Comment