Jun-02-2020, 06:21 AM
I have a code that takes the host address, asks for filename (to save on user's desktop) and asks if user's want to clear the file or append to it.
when it runs, it prints the output to screen first and then writes the file. how can I use multiproccessing to make them work simultaneously? i've tried with target= and they run but still one after another and not together.
(Don't mind the host, filename, clear input validation, they're here for testing multiproccessing)
![[Image: Q0Abr.jpg]](https://i.stack.imgur.com/Q0Abr.jpg)
when it runs, it prints the output to screen first and then writes the file. how can I use multiproccessing to make them work simultaneously? i've tried with target= and they run but still one after another and not together.
(Don't mind the host, filename, clear input validation, they're here for testing multiproccessing)
import multiprocessing as mp import subprocess as sub import sys import os class pingURL(): def __init__(self): self.t2d = mp.Queue() self.tf = mp.Queue() host = input("Enter Host: ") filename = input("Enter filename: ") clear = input("Clear File? [Y/n] ") print(clear, filename) # For Debugging Input Validation if clear.lower() == 'y' and filename == '': self.pHclrFileYesFnNo(host) # self.clrFileYesFnNoWritefile(host) # self.clrFileYesFnNoPrintoutput(host) elif clear.lower() == 'n' and filename == '': self.clrFileNoFnNoPrintoutput(host) self.clrFileNoFnNoWritefile(host) elif clear.lower() == 'y' and filename != '': self.clrFileYesFnYesPrintoutput(host) self.clrFileYesFnYesWritefile(host, filename) elif clear.lower() == 'n' and filename != '': self.clrFileNoFnYesPrintoutput(host) self.clrFileNoFnYesWritefile(host, filename) def pHclrFileYesFnNo(self, host): procs = [] proc = mp.Process(name="Clear + No Filename + Print Output", target=self.clrFileYesFnNoPrintoutput(host)) proc2 = mp.Process(name="Clear + No Filename + Write to file", target=self.clrFileYesFnNoWritefile(host)) procs.append(proc) procs.append(proc2) for proc in procs: proc.start() proc.join() def clrFileYesFnNoPrintoutput(self, host): print(f'Number of procceccess: {mp.cpu_count()}') print(f'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}') with sub.Popen(['ping', f'{host}'], stdout=sub.PIPE, bufsize=1, universal_newlines=True, stderr=sub.STDOUT) as p: for line in p.stdout: print(line, end=' ') def clrFileYesFnNoWritefile(self, host): print(f'Number of procceccess: {mp.cpu_count()}') print(f'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}') file = fr'c:/users/{os.getlogin()}/Desktop/default.txt' with open(file, 'a') as output: output.truncate(0) sub.call(['ping', f'{host}'], stdout=output) output.close() def clrFileNoFnNoPrintoutput(self, host): with sub.Popen(['ping', f'{host}'], stdout=sub.PIPE, bufsize=1, universal_newlines=True, stderr=sub.STDOUT) as p: for line in p.stdout: print(line, end=' ') def clrFileNoFnNoWritefile(self, host): file = fr'c:/users/{os.getlogin()}/Desktop/default.txt' with open(file, 'a') as output: sub.call(['ping', f'{host}'], stdout=output) output.close() def clrFileYesFnYesPrintoutput(self, host): with sub.Popen(['ping', f'{host}'], stdout=sub.PIPE, bufsize=1, universal_newlines=True, stderr=sub.STDOUT) as p: for line in p.stdout: print(line, end=' ') def clrFileYesFnYesWritefile(self, host, filename): file = fr'c:/users/{os.getlogin()}/Desktop/{filename}.txt' with open(file, 'a') as output: output.truncate(0) sub.call(['ping', f'{host}'], stdout=output) output.close() def clrFileNoFnYesPrintoutput(self, host): with sub.Popen(['ping', f'{host}'], stdout=sub.PIPE, bufsize=1, universal_newlines=True, stderr=sub.STDOUT) as p: for line in p.stdout: print(line, end=' ') def clrFileNoFnYesWritefile(self, host, filename): file = fr'c:/users/{os.getlogin()}/Desktop/{filename}.txt' with open(file, 'a') as output: sub.call(['ping', f'{host}'], stdout=output) output.close() if __name__ == "__main__": pingURL()I get the same Proccess name and ID when running the script:
![[Image: Q0Abr.jpg]](https://i.stack.imgur.com/Q0Abr.jpg)