Python-wait for one process to finish before starting next one
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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")
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")
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.