Need threaded python script

I have a single threaded bash script that I am using to create secgroup rules in openstack. The process to add the rules is taking forever. Any of you python gurus know how to convert this bash script into a thread python script? Thanks in advanced.

create-secgroup-rules.sh:

#!/bin/bash

cat  tcp-ports.txt | while read line 
do
service=`echo $line | awk -F, '{print $1}'`
port=`echo $line | awk -F, '{print $2}'`
proto=`echo $line | awk -F, '{print $3}'`
cidr="0.0.0.0/0"

for i in `cat secgroups.txt`;
do
/usr/bin/nova secgroup-add-rule $i $proto $port $port $cidr
done 
done

Input files

tcp-ports.txt:

ftp,21,tcp
ssh,22,tcp
telnet,23,tcp
smtp,25,tcp

secgroups.txt:

secgroup1
secgroup2
secgroup3

Your shell script is very poorly implemented. Perhaps an approach that isn't so inefficient would suffice.

while IFS=, read -r service port proto; do
    while read -r secgroup; do
        /usr/bin/nova secgroup-add-rule "$secgroup" "$proto" "$port" "$port" 0.0.0.0/0
    done < secgroups.txt
done < tcp-ports.txt

I did not test that, so there may be a typo.

Also, your code doesn't appear to use $service but uses $port twice in nova's command options.

If you want to speed it up further, don't use bash. If your system's sh is something else, try it (dash or ksh, for example, are faster than bash).

If you use ksh or bash, you can use arrays to speed up the inner loop.

Regards,
Alister

Thank you, Alister! I will give your script a try.