LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-14-2018, 08:40 AM   #1
linux starter
LQ Newbie
 
Registered: Feb 2018
Posts: 2

Rep: Reputation: Disabled
Python-wait for one process to finish before starting next one


Hello,

i'm new to python

I have script which go through JSON file,look for values in email field, for each value found it should create new EC2 instance with field in email adrress (part after @ removed) as user name, for every user it should prompt for number of instances.It all works fine when one user is in JSON file, but how to make python process one user at a time (if we have more than one email address in JSON file, first create machine for one user,wait until finishes, then prompt for number of instances for other user, wait until machine is created, then prompt for number of instances for 3-rd user, and so on


Code:
#!/bin/python
import json
from pprint import pprint

json_data=open('./my.json')
data = json.load(json_data)

json_data.close()

for i in range (0, len (data['customers'])):
    #print data['customers'][i]['email']
    k=data['customers'][i]['email']
    #print(k.split('@')[0])
    user=k.split('@')[0]
    print(user)


enter = int(input('Enter number of instances: '))
from python_terraform import *
tf = Terraform(working_dir='/home/ja/terraform-course/demo-2b', variables={'count':enter,'INSTANCE_USERNAME':user})
tf.plan(no_color=IsFlagged, refresh=False, capture_output=True)
approve = {"auto-approve": True}
print(tf.plan())
print(tf.apply(**approve))
print("All is done for user")
 
Old 02-15-2018, 09:49 PM   #2
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
If I understand this correctly..

Take each "customer" found in data['customers'] and run the below code?

Code:
enter = int(input('Enter number of instances: '))
from python_terraform import *
tf = Terraform(working_dir='/home/ja/terraform-course/demo-2b', variables={'count':enter,'INSTANCE_USERNAME':user})
tf.plan(no_color=IsFlagged, refresh=False, capture_output=True)
approve = {"auto-approve": True}
print(tf.plan())
print(tf.apply(**approve))
print("All is done for user")
Perhaps just include it in the for loop?

Code:
for i in data['customers']:
    #print data['customers'][i]['email']
    k=data['customers'][i]['email']
    #print(k.split('@')[0])
    user=k.split('@')[0]
    print(user)

    enter = int(input('Enter number of instances: '))                           
    from python_terraform import *                                              
    tf = Terraform(working_dir='/home/ja/terraform-course/demo-2b', variables={'count':enter,'INSTANCE_USERNAME':user})
    tf.plan(no_color=IsFlagged, refresh=False, capture_output=True)             
    approve = {"auto-approve": True}                                            
    print(tf.plan())                                                            
    print(tf.apply(**approve))                                                  
    print("All is done for user")
 
1 members found this post helpful.
Old 02-16-2018, 12:24 PM   #3
linux starter
LQ Newbie
 
Registered: Feb 2018
Posts: 2

Original Poster
Rep: Reputation: Disabled
thanks,

i just created function and count put in JSON file


#!/bin/python
import json
from pprint import pprint
from python_terraform import *




def myfunc():

tf = Terraform(working_dir='/home/ja/terraform-course/demo-2b', variables={'count':count,'INSTANCE_USERNAME':user})
tf.plan(no_color=IsFlagged, refresh=False, capture_output=False)
approve = {"auto-approve": True}
print(tf.plan())
print(tf.apply(**approve))
return

json_data=open('./my.json')
data = json.load(json_data)

json_data.close()

for i in range (0, len (data['customers'])):
#print data['customers'][i]['email']
k=data['customers'][i]['email']
#print(k.split('@')[0])
user=k.split('@')[0]
#print(user)
count=data['customers'][i]['instances']
#print(count)
#enter = int(input('Enter number of instances: '))
myfunc()

Last edited by linux starter; 02-16-2018 at 12:27 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Wait for “git clone” to finish in bash luftwaffe Linux - Software 3 05-25-2016 11:18 AM
Python: easiest way to wait for whichever child process completes first? johnsfine Programming 11 06-27-2014 01:05 AM
Wait for a remote script to finish before executing another vikas027 Programming 4 08-07-2012 01:48 AM
Wait for one process to stop before starting another? Jykke Linux - Software 3 07-13-2011 09:37 AM
Pppd does not wait for chat script to finish ruchika Programming 1 08-28-2003 11:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration