Python Forum
Stuck on multiproccessing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stuck on multiproccessing
#1
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)

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]
Reply
#2
I noticed you are passing an already called method as the target
proc = mp.Process(name="Clear + No Filename + Print Output", target=self.clrFileYesFnNoPrintoutput(host))
the process itself should do the calling
proc = mp.Process(name="Clear + No Filename + Print Output", target=self.clrFileYesFnNoPrintoutput, , args=(host,))
Reply
#3
Well, that did it.
Thank you! :)
can you show me how can I use the pool/map with it?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020