我有一个使用主机地址的代码,要求输入文件名(保存在用户的桌面上,并询问用户是否要清除文件或将其追加。)>
运行时,它首先将输出打印到屏幕上,然后写入文件。如何使用多进程使它们同时工作?我已经尝试过target =并且它们可以运行,但仍然是一个接一个,而不是在一起。
...................................................... ................................................... ....................................]
((不用管主机名,文件名,清除输入验证,它们在这里用于测试多进程)
代码:(我用第一个条件测试了多进程)
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()
运行脚本时,我得到相同的过程名称和ID:
我有一个使用主机地址的代码,要求输入文件名(保存在用户的桌面上),并询问用户是否要清除文件或将其追加。当它运行时,它将输出首先打印到屏幕上...
好,解决方案是为每个条件定义一个处理程序。对我有用。