Saya memiliki kode yang mengambil alamat host, meminta nama file (untuk disimpan di desktop pengguna) dan menanyakan apakah pengguna ingin menghapus file atau menambahkannya.

Ketika dijalankan, ia mencetak output ke layar terlebih dahulu dan kemudian menulis file. bagaimana saya bisa menggunakan multiprocessing untuk membuatnya bekerja secara bersamaan? saya sudah mencoba dengan target= dan mereka berjalan tetapi masih satu demi satu dan tidak bersama.

................................................................... ................................................................... ...................................

(Jangan pedulikan Host, nama file, validasi input yang jelas, mereka ada di sini untuk menguji multiprosesor)

Kode: (Saya menggunakan kondisi pertama untuk menguji multiproses)

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()

Saya mendapatkan nama dan ID Proses yang sama saat menjalankan skrip:

enter image description here

0
Gilush 2 Juni 2020, 02:42

1 menjawab

Jawaban Terbaik

Baik solusinya adalah mendefinisikan penangan untuk setiap kondisi. yang bekerja untuk saya.

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()
0
Gilush 2 Juni 2020, 17:55