I've managed to use multiprocessing to print the stdout to file and my terminal
but i'm lost when trying to redirect the stdout to the text box in realtime.
i'm seeing double outputs (i know, the print() does that, i want to redirect it to the textbox)
Processes:
![[Image: 0kzQd.jpg]](https://i.stack.imgur.com/0kzQd.jpg)
what do I need to add to this block in order for it to work?
class code:
TextBox:
but i'm lost when trying to redirect the stdout to the text box in realtime.
i'm seeing double outputs (i know, the print() does that, i want to redirect it to the textbox)
Processes:
![[Image: 0kzQd.jpg]](https://i.stack.imgur.com/0kzQd.jpg)
what do I need to add to this block in order for it to work?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
@classmethod def generate_clr_yes_fn_no_print_output_to_gui( self , host): print ( f 'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}\n' ) cmd = [ "ping" , "-n" , "4" , f '{host}' ] proc = sub.Popen(cmd, stdout = sub.PIPE, shell = True , stderr = sub.STDOUT) for stdout_line in proc.stdout: print (stdout_line.decode(), end = '') yield stdout_line proc.stdout.close() return_code = proc.wait() if return_code: raise sub.CalledProcessError(return_code, cmd) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
class TestPing(tk.Frame): def __init__( self , parent, controller): tk.Frame.__init__( self , parent, bg = "skyblue1" ) self .controller = controller self .clear_file = tk.BooleanVar() self .clear_file. set ( False ) self .url_label = tk.Label( self , text = "Enter URL : " , padx = 7 , pady = 5 , bg = "skyblue1" ) self .url_input_box = tk.Entry( self ) self .file_name_label = tk.Label( self , text = "Enter Filename: " , bg = "skyblue1" ) self .file_name_input_box = tk.Entry( self ) self .clear_file_label = tk.Label( self , text = "Clear File?" , padx = 5 , pady = 5 , bg = "skyblue1" ) self .clear_file_radio_yes = tk.Radiobutton( self , text = "yes" , value = True , var = self .clear_file, bg = "skyblue1" , command = lambda : self .callback( self .clear_file.get())) self .clear_file_radio_no = tk.Radiobutton( self , text = "no" , value = False , var = self .clear_file, bg = "skyblue1" , command = lambda : self .callback( self .clear_file.get())) self .submit_button = tk.Button( self , text = "Submit" , width = 10 , height = 1 , command = lambda : self .condition( self .url_input_box.get(), self .clear_file.get(), self .file_name_input_box.get())) self .clear_fields_button = tk.Button( self , text = "Clear Fields" , width = 10 , command = lambda : self .clear_boxes( self .url_input_box, self .file_name_input_box)) self .c = tk.Canvas( self , height = 300 , width = 500 , bg = "gray" ) self .grid_columnconfigure( 0 , minsize = 50 , weight = 0 ) self .grid_columnconfigure( 1 , minsize = 20 , weight = 2 ) self .grid_columnconfigure( 2 , minsize = 50 , weight = 3 ) self .grid_columnconfigure( 3 , minsize = 20 , weight = 2 ) self .grid_rowconfigure( 0 , minsize = 50 , weight = 0 ) self .grid_rowconfigure( 1 , minsize = 20 , weight = 0 ) self .grid_rowconfigure( 2 , minsize = 30 , weight = 0 ) self .grid_rowconfigure( 3 , minsize = 10 , weight = 0 ) self .grid_rowconfigure( 4 , minsize = 10 , weight = 0 ) self .grid_rowconfigure( 5 , minsize = 10 , weight = 0 ) self .url_label.grid(row = 0 , column = 0 , sticky = "w" ) self .url_input_box.grid(row = 0 , column = 1 , sticky = "w" ) self .file_name_label.grid(row = 1 , column = 0 , sticky = "w" ) self .file_name_input_box.grid(row = 1 , column = 1 , sticky = "w" ) self .clear_file_label.grid(row = 0 , column = 1 , sticky = "n" ) self .clear_file_radio_yes.grid(row = 1 , column = 1 ) self .clear_file_radio_no.grid(row = 2 , column = 1 ) self .submit_button.grid(row = 3 , column = 1 , sticky = "se" ) self .clear_fields_button.grid(row = 3 , column = 1 , sticky = "sw" ) self .c.grid(row = 7 , column = 1 , sticky = "swe" ) @classmethod def clear_boxes( self , urlInputBox, fileNameInputBox): urlInputBox.delete( 0 , "end" ) fileNameInputBox.delete( 0 , "end" ) @classmethod def callback( self , clearFile): print ( f 'Clear file = {clearFile}' ) # Debugging Mode - check Radio box Var. def condition( self , host, clearFile, filenameInputBox): print (clearFile, filenameInputBox) # Debugging - Input Validation if clearFile is True and filenameInputBox = = '': self .handler_clr_yes_fn_no(host) elif clearFile is False and filenameInputBox = = '': self .handler_clr_no_fn_no(host) elif clearFile is True and filenameInputBox ! = '': self .handler_clr_yes_fn_yes(host, filenameInputBox) elif clearFile is False and filenameInputBox ! = '': self .handler_clr_no_fn_yes(host, filenameInputBox) def handler_clr_yes_fn_no( self , host): startprocs = [] lastprocs = [] proc1 = mp.Process(name = "Clear + No Filename + WriteFile" , target = self .clr_yes_fn_no_writefile, args = (host,)) proc2 = mp.Process(name = "Clear + No Filename + PrintOutput" , target = self .clr_yes_fn_no_print_output, args = (host,)) proc3 = mp.Process(name = "Clear + No Filename + Generate PrintOutput to GUI" , target = self .generate_clr_yes_fn_no_print_output_to_gui, args = (host,)) proc4 = mp.Process(name = "Remove first line + Write new default file" , target = self .delete_default_lines) startprocs.append(proc1) startprocs.append(proc2) startprocs.append(proc3) lastprocs.append(proc4) for s in startprocs: s.start() for s2 in startprocs: s2.join() for l in lastprocs: l.start() def handler_clr_no_fn_no( self , host): procs = [] nextprocs = [] proc1 = mp.Process(name = "Append to default file" , target = self .clr_no_fn_no_writefile, args = (host,)) proc2 = mp.Process(name = "Print Output" , target = self .clr_no_fn_no_printoutput, args = (host,)) procs.append(proc1) procs.append(proc2) for proc in procs: proc.start() for proc in procs: proc.join() for p in nextprocs: p.start() def handler_clr_yes_fn_yes( self , host, filenameInputBox): procs = [] nextprocs = [] proc1 = mp.Process(name = "Clear file + userFilename + Write to file" , target = self .clr_yes_fn_yes_writefile, args = (host, filenameInputBox,)) proc2 = mp.Process(name = "Clear file + user filename + Print output" , target = self .clr_yes_fn_yes_printoutput, args = (host,)) proc3 = mp.Process(name = "Remove Empty Lines from user filename" , target = self .delete_userfile_lines, args = (filenameInputBox,)) procs.append(proc1) procs.append(proc2) nextprocs.append(proc3) for proc in procs: proc.start() for p in procs: p.join() for np in nextprocs: np.start() def handler_clr_no_fn_yes( self , host, filenameInputBox): procs = [] proc1 = mp.Process(name = "Keep File + Userfilename + Append to Userfile" , target = self .clr_no_fn_yes_writefile, args = (host, filenameInputBox,)) proc2 = mp.Process(name = "Keep File + Userfilename + Print Output" , target = self .clr_no_fn_yes_printoutput, args = (host,)) procs.append(proc1) procs.append(proc2) for p in procs: p.start() for p2 in procs: p2.join() @classmethod def delete_default_lines( cls ): time.sleep( 1.5 ) print ( f '\nCurrent Proccess: {mp.current_process().name} + {mp.current_process().pid}' ) file = fr 'c:/users/{os.getlogin()}/Desktop/Gui-Skeleton/default-tmp.txt' newfile = fr 'c:/users/{os.getlogin()}/Desktop/default.txt' with open ( file , 'r' ) as inp, open (newfile, 'w+' ) as out: for line in inp: if not line.isspace(): out.write(line.lstrip()) out.write('') inp.close() out.close() os.remove( file ) @classmethod def delete_userfile_lines( cls , filename): time.sleep( 1.5 ) print ( f 'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}' ) file = fr 'c:/users/{os.getlogin()}/Desktop/Gui-Skeleton/{filename}-tmp.txt' newfile = fr 'c:/users/{os.getlogin()}/Desktop/{filename}.txt' with open ( file , 'r+' ) as inp, open (newfile, 'w+' ) as out: for line in inp: if not line.isspace(): out.write(line.lstrip()) out.write('') inp.close() out.close() os.remove( file ) @classmethod def clr_yes_fn_no_print_output( self , host): print ( f 'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}\n' ) with sub.Popen([ "ping" , "-n" , "4" , f '{host}' ], stdout = sub.PIPE, bufsize = 1 , universal_newlines = True , stderr = sub.STDOUT) as p: for line in p.stdout: print (line, end = ' ' ) @classmethod def generate_clr_yes_fn_no_print_output_to_gui( self , host): print ( f 'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}\n' ) cmd = [ "ping" , "-n" , "4" , f '{host}' ] proc = sub.Popen(cmd, stdout = sub.PIPE, shell = True , stderr = sub.STDOUT) for stdout_line in proc.stdout: print (stdout_line.decode(), end = '') # yield stdout_line proc.stdout.close() return_code = proc.wait() if return_code: raise sub.CalledProcessError(return_code, cmd) @classmethod def clr_yes_fn_no_print_output_to_gui( self ): tk.Frame.__init__( self , parent) self .textbox.insert(tk.END, proc.stdout.decode()) @classmethod def clr_yes_fn_no_writefile( self , host): print ( f 'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}\n' ) file = fr 'c:/users/{os.getlogin()}/Desktop/Gui-Skeleton/default-tmp.txt' ping = sub.Popen([ "ping" , "-n" , '4' , f '{host}' ], stdout = sub.PIPE) with open ( file , 'w+' ) as output: data = output.read() for line in ping.stdout.readlines(): data + = str (line.decode()) ping.stdout.close() output.seek( 0 ) output.write(data.lstrip()) @classmethod def clr_no_fn_no_printoutput( self , host): print ( f 'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}\n' ) with sub.Popen([ "ping" , "-n" , '4' , f '{host}' ], stdout = sub.PIPE, bufsize = 1 , universal_newlines = True , stderr = sub.STDOUT) as p: for line in p.stdout: print (line, end = ' ' ) @classmethod def clr_no_fn_no_writefile( self , host): print ( f 'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}\n' ) with open (fr 'c:/users/{os.getlogin()}/Desktop/default.txt' , 'a' ) as output: sub.call([ "ping" , "-n" , '4' , f '{host}' ], stdout = output) @classmethod def clr_yes_fn_yes_printoutput( self , host): with sub.Popen([ "ping" , "-n" , '4' , f '{host}' ], stdout = sub.PIPE, bufsize = 1 , universal_newlines = True , stderr = sub.STDOUT) as p: for line in p.stdout: print (line, end = ' ' ) @classmethod def clr_yes_fn_yes_writefile( self , host, filename): file = fr 'c:/users/{os.getlogin()}/Desktop/Gui-Skeleton/{filename}-tmp.txt' with open ( file , 'w' ) as output: sub.call([ "ping" , "-n" , '4' , f '{host}' ], stdout = output) @classmethod def clr_no_fn_yes_printoutput( self , host): with sub.Popen([ "ping" , "-n" , '4' , f '{host}' ], stdout = sub.PIPE, bufsize = 1 , universal_newlines = True , stderr = sub.STDOUT) as p: for line in p.stdout: print (line, end = ' ' ) @classmethod def clr_no_fn_yes_writefile( self , host, filename): with open (fr 'c:/users/{os.getlogin()}/Desktop/{filename}.txt' , 'a' ) as output: sub.call([ "ping" , "-n" , '4' , f '{host}' ], stdout = output) |
![[Image: trSgs.jpg]](https://i.stack.imgur.com/trSgs.jpg)