Python Forum
a little project idea
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a little project idea
#1
here's an idea for a little project:  a command that takes 2 (or maybe more, if you want to implement that) commands, in the arguments (or elsewhere, whatever you decide), and executes them in parallel in separate processes, and waits for all of them to be done before exiting.

i have done this in C, but a Python version would be nice example code to have around.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Sure.  How's this?
import shlex, sys, subprocess

def SpinTask(task):
    task = shlex.split(task)
    proc = subprocess.Popen(task, stdout=subprocess.PIPE)
    return proc
    
help_text = '''
Feed me some commands!  
I go do them, and return when they're all done!
ANY OUTPUT IS CONSUMED!  MY HUNGER IS INSATIABLE!
'''
if __name__ == '__main__':
    programs = sys.argv[1:]
    if not programs:
        print(help_text)
    else:
        # start things running
        processes = [SpinTask(process) for process in programs]
        # now that they're all running, wait for them to finish
        [process.wait() for process in processes]
        
asyncio can also spawn subprocesses, so that could be a cool way for someone to do it.
Reply
#3
(Oct-19-2016, 12:27 AM)Skaperen Wrote: here's an idea for a little project:  a command that takes 2 (or maybe more, if you want to implement that) commands, in the arguments (or elsewhere, whatever you decide), and executes them in parallel in separate processes, and waits for all of them to be done before exiting.

i have done this in C, but a Python version would be nice example code to have around.

I've done that in bash.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#4
@nilamo looks good to me

my C version had a process pool size limit and would substitute each line of input (stdin) into a marker character in the command pattern. an option could be used to use a different marker character. i never thought to try it in bash. but now i would do it in python. i plan to convert lots of things i have done in either C or bash, to python.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  a project idea Skaperen 7 7,532 Oct-12-2016, 03:21 AM
Last Post: wavic

Forum Jump:

User Panel Messages

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