1

I have a python script which takes one input at a time and process it . I want to run script parallel with different inputs at a same time . Like 50 or 100 times but with different inputs feed from txt file .

i execute script like:

python script.py -d url1 

then

python script.py -d url2

then

python script.py -d url3

and instead of input one argument at a time , i want to feed those url from a text file and process them in parallel .

I tried this script running in bash shell using gnu-parallel but bash script not runs python shell and thus errors.

the code is as follows---

#!/usr/bin/env bash
doit() {
    host="$1"
    ~/script/python script1.py -d $host
      }
   export -f doit

   cat "$1" | parallel -j50 -k doit 

contents of txt file---

url1.com
url2.com
url3.com
--------
url1000.com
url_any.com
2
  • 1
    I downvoted because it seems no research attempt was made. Commented Sep 4, 2018 at 14:47
  • i tried to run it with with gnu parallel but before , i run parallel in bash script like go language accepts but this needs shell in python and thus i can't able to run it . Commented Sep 4, 2018 at 15:00

2 Answers 2

6

With GNU Parallel, like this:

parallel --dry-run -a arguments.txt python script.py

which assumes your arguments are one per line in "arguments.txt".

Use parallel -k ... to keep outputs in order, if required.

Use parallel --bar ... to get a progress bar.

Sign up to request clarification or add additional context in comments.

7 Comments

python script itself takes argument like "python script1.py -d abc"
Not sure I understand. Just add any parameters you need at the end. Please click edit under your question and show 3 lines of your "arguments.txt" file and then show the 3 corresponding commands that should be executed.
i tried your code and it just output the text required command rather then executing that paricular command like " python script.py - d any.com "
If the output looks correct, run it again but without --dry-run which is for debugging.
yeah its working , can it take like -j50 or -j100 like argument to control input.
|
0

An alternative to GNU Parallel is to use Python subprocess to execute the command repeatedly.

This is an example using Python 2.7.

First your code will need to read the text file to assign all the arguments to a list.

with open('<Arguments text file location>') as f:
    arguments = f.read().splitlines()

Then you use a loop to run the command once for each argument using subprocess.

import subprocess
procs = []
for argument in arguments:
    cmd = 'python script.py %(argument)' % {'argument': argument}
    procs.append(
        subprocess.Popen(cmd, shell=True)
    )
exit_codes = [p.wait() for p in procs]

2 Comments

if no. of arguments are say 1000, this would create 1000 shell processes right? is that a thing to worry about? Is there any limit to no of sub processes we can spawn without crashing anything?
AFAIK the limit is your CPU and memory. You need to do some benchmarking for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.