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)
Code: (I used the first condition to test the multiproccess)
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:
well the solution was defining a handler for every condition. that worked for me.
def pHclrFileNoFnNo(self, host):
procs = []
proc = mp.Process(target=self.clrFileNoFnNoWritefile, args=(host,))
proc.daemon = False
proc2 = mp.Process(target=self.clrFileNoFnNoPrintoutput, args=(host,))
proc2.daemon = True
procs.append(proc)
procs.append(proc2)
for proc in procs:
proc.start()