Need threaded python script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need threaded python script
# 1  
Old 04-10-2014
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:
Code:
#!/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:
Code:
ftp,21,tcp
ssh,22,tcp
telnet,23,tcp
smtp,25,tcp

secgroups.txt:
Code:
secgroup1
secgroup2
secgroup3


Last edited by Franklin52; 04-10-2014 at 03:04 AM.. Reason: Please use code tags
# 2  
Old 04-10-2014
Your shell script is very poorly implemented. Perhaps an approach that isn't so inefficient would suffice.
Code:
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

Last edited by alister; 04-10-2014 at 02:06 AM..
# 3  
Old 04-10-2014
Thank you, Alister! I will give your script a try.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

How to execute python script on remote with python way..?

Hi all, I am trying to run below python code for connecting remote windows machine from unix to run an python file exist on that remote windows machine.. Below is the code I am trying: #!/usr/bin/env python import wmi c = wmi.WMI("xxxxx", user="xxxx", password="xxxxxxx")... (1 Reply)
Discussion started by: onenessboy
1 Replies

2. Shell Programming and Scripting

script for multi-threaded bash processes

hey everyone, I'm having some trouble breaking down some code. It's simple a control script that takes machines meant to be backed up from a list. Then according to that will run multi-threaded processes up until the specified thread limit. for example if there are 4 machines to be backed up,... (2 Replies)
Discussion started by: terrell
2 Replies

3. Programming

multi-threaded memory leak

Hello All : I write a .c program to test the exactually resource the memory leak as follows: 1 #include <stdio.h> 2 #define NUM 100000 3 void *Thread_Run(void * arg){ 4 //TODO 5 //pthread_datch(pthread_self()); 6 int socket= (int)arg; 7 ... (1 Reply)
Discussion started by: aobai
1 Replies

4. Linux

Multi-threaded encryption @ Fedora 11

Hello, are any of the encryption programs capable of true multi-threading ? Friend of mine tells me that he's been running some testing on Fedora 11 and that the kernel doesn't support multi-threading at that level. I've been looking into TrueCrypt, encfs and both calm to support... (1 Reply)
Discussion started by: TehOne
1 Replies

5. UNIX for Advanced & Expert Users

Multi-threaded encryption @ Fedora 11

Hello, are any of the encryption programs capable of true multi-threading ? Friend of mine tells me that he's been running some testing on Fedora 11 and that the kernel doesn't support multi-threading at that level. I've been looking into TrueCrypt, encfs and both calm to support... (0 Replies)
Discussion started by: TehOne
0 Replies

6. Programming

help me out with my threaded c++ mudbase - c++, pthread_cond_wait

hello, in my free time i am writing on a c++ mud codebase, a while ago i decided that i would move to pthreads, so i could make use of smp. now i have a problem which i just cant fix - for weeks now. i have a main thread which spawns my threads, as soon as spawned they get a pthread_cond_wait, so... (4 Replies)
Discussion started by: sonicx
4 Replies

7. AIX

multi threaded program is hanging

I have a Multithreaded program which is hanging on AIX. OS Version: AIX 5.2 and thread library version : 5.2.0.75 We Initiate the process with 50 threads..when we are disconnecting from the process it hangs.There is lots of other stuff involved here.I am just sending the piece of the problem with... (0 Replies)
Discussion started by: hikrishn
0 Replies

8. Programming

threaded merge sort help

I am working on a merge sort of two files of integers, and am fuzzy on some of the logic\syntax. I need two threads, each of which will open a file, read its contents into an array, and then sort the array using qsort. One thread will operate on file f1.dat(10000 numbers) and leave its sorted... (0 Replies)
Discussion started by: AusTex
0 Replies

9. Programming

Threaded 'find' utility

I need to modify my version of find in unix and get it to create and use two POSIX threads to carry out concurrent finding operations. How do i get about doing this>? If anyone could help me it would be much appreciated. Thanx Mariuca (1 Reply)
Discussion started by: mariuca
1 Replies

10. UNIX for Dummies Questions & Answers

Threaded Discussions for Webpages

Dear All, I run a website for a non-profit. Does anyone know where I can get free or cheap software to run threaded discussions for our website? Our website is obviously running off a unix platform. Thanks (4 Replies)
Discussion started by: evertk
4 Replies
Login or Register to Ask a Question