I have a problem with waiting sub-process running from within python.I've read tons of info about it, here as well. Sorry to lift this question ones again but still no solution for me.
My code
cmds = "cd /etc/openvpn/easy-rsa && . ./vars && ./clean-all && ./pkitool --initca && ./pkitool --server && ./build-dh"
runCmds = subprocess.Popen(cmds, shell=True)
# run = os.system
# runCmds = run(cmds)
# runCmds.wait()
# runCmds.call() 
works perfect, but I need it wait subprocesses ending to run next portion of code. Commented lines not work for me. If I run something from commented I get error
Please source the vars script first (i.e. "source ./vars") .......
One time it seems wait() worked but after some time don't. Method call() runs commands but never ends. Why methods not working for me, especially wait()? I suggest my problem is somewhere in sourcing openvpn vars script in my environment. Please help me!
UPDATED: console log with 
set -x
  [25/Jan/2016 18:30:42]"POST /run-step3-process/ HTTP/1.1" 200 49
+ cd /etc/openvpn/easy-rsa
+ . ./vars
+ pwd
+ export EASY_RSA=/etc/openvpn/easy-rsa
+ export OPENSSL=openssl
+ export PKCS11TOOL=pkcs11-tool
+ export GREP=grep
+ /etc/openvpn/easy-rsa/whichopensslcnf /etc/openvpn/easy-rsa
+ export KEY_CONFIG=/etc/openvpn/easy-rsa/openssl-1.0.0.cnf
+ export KEY_DIR=/etc/openvpn/easy-rsa/keys
+ echo NOTE: If you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/easy-rsa/keys
NOTE: If you run ./clean-all, I will be doing a rm -rf on /etc/openvpn/easy-rsa/keys
+ export PKCS11_MODULE_PATH=dummy
+ export PKCS11_PIN=dummy
+ export KEY_SIZE=1024
+ export CA_EXPIRE=3650
+ export KEY_EXPIRE=3650
+ export KEY_COUNTRY=SS
+ export KEY_PROVINCE=FFFFFFF
+ export KEY_CITY=AAAA
+ export KEY_ORG=GGGG
+ export KEY_EMAIL=qq@qq.yy
+ export KEY_EMAIL=mail@host.domain
+ export KEY_CN=ccccccc
+ export KEY_NAME=changeme
+ export KEY_OU=changeme
+ export PKCS11_MODULE_PATH=changeme
+ export PKCS11_PIN=1234
+ ./clean-all
+ ./pkitool --initca
Using CA Common Name: ccccccc
Generating a 1024 bit RSA private key
.................................++++++
............................................++++++
writing new private key to 'ca.key'
-----
+ ./pkitool --server
Using Common Name: ccccccc
Generating a 1024 bit RSA private key
............++++++
...........................++++++
writing new private key to 'ccccccc.key'
-----
Using configuration from /etc/openvpn/easy-rsa/openssl-1.0.0.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
countryName           :PRINTABLE:'SS'
stateOrProvinceName   :PRINTABLE:'FFFFFFF'
localityName          :PRINTABLE:'AAAA'
organizationName      :PRINTABLE:'GGGG'
organizationalUnitName:PRINTABLE:'changeme'
commonName            :PRINTABLE:'ccccccc'
name                  :PRINTABLE:'changeme'
emailAddress          :IA5STRING:'mail@host.domain'
Certificate is to be certified until Jan 22 18:30:42 2026 GMT (3650 days)
Write out database with 1 new entries
Data Base Updated
+ ./build-dh
Generating DH parameters, 1024 bit long safe prime, generator 2
This is going to take a long time
.....................................+...+..................................................................................................................................+................+..................................++*++*++*
UPDATE 2 I have researched my code's behaviour:
Before run:
1.apache(or localhost)restart
2.etc/openvpn/easy-rsa/keys clean
3.start browser with new incognito window
I give permission for etc/openvpn/easy-rsa/ as 47777
success == generating process run, new keys create
error == “please source ./vars.....”
CN == variable for server name in vars 
wait() == subrpocess.wait() code following string with bash commands
code ALWAYS work as below:
orig vars -> edit -> wait() -> error
orig vars -> edit (without CN, ./pkitool --server SERVER )-> wait() -> error
orig vars -> NONedit ->wait() -> success
orig vars -> edit ->WITHOUT_wait() -> success
edited vars -> edit ->WITHOUT_wait() -> success
edited vars -> edit(without CN, ./pkitool --server SERVER) -> WITHOUT_wait() -> success
orig vars -> edit(WITH_ CN, ./pkitool --server) -> WITHOUT_wait() -> success
edited vars -> edit(WITH_ CN, ./pkitool --server) -> WITHOUT_wait() -> success
I edit vars in python:
from django.shortcuts import  HttpResponse, HttpRequest
import subprocess
from subprocess import Popen, PIPE
import json
import os.path
def pass3Cmds():
''' run commands on step3  to generate keys and cert in '/etc/openvpn/easy-     rsa/keys'
'''
    cmds = "cd /etc/openvpn/easy-rsa && . ./vars && ./clean-all && ./pkitool --initca && ./pkitool --server && ./build-dh"
    runCmds = subprocess.Popen(cmds, shell=True)
def runStep3Process(request):
    '''collect data from step3 user form and insert 
    them in '/etc/openvpn/easy-rsa/vars'
    '''
    path = '/etc/openvpn/easy-rsa/vars'
    data = json.loads(request.body)
    key_cn = 'export KEY_CN="%s"' % data['key_cn']
    if request.method=='POST' and request.user.is_authenticated():
        with open(path) as varsfile:
            data = varsfile.readlines()
        try:
            data[69] = key_cn +'\n' 
            with open(path, 'w') as newvarsfile:
                newvarsfile.writelines(data)
                pass3Cmds()
                pem = '/etc/openvpn/easy-rsa/keys/dh1024.pem'
                if os.path.exists(pem):
                    return HttpResponse(successMsg2)
                return HttpResponse(dangerMsg)
        except IndexError:
            return HttpResponse(warnMsg2)
     return HttpResponse(warnMsg)
Ones again: code perfectly works with this way edited vars until I want run any code to wait subprocess. If I run e.g. subprocess.wait() I get "please source ./vars" error
Question is: why editing vars in my case force error?
 
     
     
    