Python subprocess.Popen() with Pygame , how to tell Pygame wait untill subprocess is done


I have a major issue that too many Pygame users has, I want to take input from user in Pygame. I probably check all informations in internet,include stackoverflow, nothing solved.

So I decide to make a solution, I created another Python script(I convert it to .exe so subprocess can open it) that asking question to user before Pygame is running, after then that script saving user's data into a .txt file(like a database).Then in Pygame I opening that .txt file and taking the data. Therocially it's working, but problem is, I have to tell Pygame, while subprocess is processing, wait for it. That's how I can dodge errors like IndexError etc. Because I'm using <em>readlines()</em> and untill that .exe file is closed, Pygame has to wait, if not; <em>readlines()</em> giving data as <em>list</em> and it throwing IndexError normally. So Pygame has to wait untill I put the data in that .exe file. Let me explain with my codes;

This is the script that taking data from user(I converted it to exe already so subprocess can open):

#!/usr/bin/env python # -*-coding:utf-8-*- while True: user=input("Please enter your name: ") try: user_age=int(input("Please enter your age: ")) except ValueError: print ("Invalid characters for 'age', try again.") continue ask_conf=input("Are you confirm these informations?(Y/N): ") if ask_conf.lower()=="y": with open("informations.txt","w") as f: #create the file f.write(user+"\n") f.write(str(user_age)) break else: continue

And then, in Pygame, I'am opening this .exe file, but Pygame won't wait and normally I'm getting error.

pygame.init() subprocess.Popen(["wtf.exe"]) #the exe file that taking data from user #and saving it in "informations.txt" #codes.. .... .... author = pygame.font.SysFont("comicsansms",25) with open("informations.txt") as f: rd=f.readlines() author1 = author.render("{}".format(rd[0][0:-1]),True,blue) #Pygame won't wait #so getting IndexError here age = pygame.font.SysFont("comicsansms",25) age1 = age.render("{}".format(rd[1]),True,blue) ... ... ... gameDisplay.blit(author1,(680,15)) gameDisplay.blit(age1,(720,40))

This method is almost working, I thought finally I got the solution of this <em>input</em> problem in Pygame.But I don't know how to tell Pygame, wait untill I'm done with .exe file, then process your codes.


Use the <a href="https://docs.python.org/2/library/subprocess.html#subprocess.Popen.communicate" rel="nofollow">communicate</a> method:


Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. The optional input argument should be a string to be sent to the child process, or None, if no data should be sent to the child.


So this is the code to do it:

process = subprocess.Popen(["wtf.exe"]) # block until process done output, errors = process.communicate() <hr /><h2>Edit:</h2>

As @Padraic suggests, you can use <a href="https://docs.python.org/2/library/subprocess.html#subprocess.check_call" rel="nofollow">check_call</a>, which is <a href="https://stackoverflow.com/questions/14106720/python-subprocess-call-check-call-and-returncode-to-find-if-a-command-exist" rel="nofollow">more informative</a> when failing:

try: subprocess.check_call([’wtf.exe’]) except subprocess.CalledProcessError: pass # handle errors in the called executable except OSError: pass # executable not found <hr />

Another option is <a href="https://docs.python.org/2/library/subprocess.html#subprocess.call" rel="nofollow">call</a>:


Run the command described by args. Wait for command to complete, then return the returncode attribute.


So something like this:

returncode = subprocess.call(["wtf.exe"]) <hr />

And you can also use <a href="https://docs.python.org/2/library/subprocess.html#subprocess.Popen.wait" rel="nofollow">wait</a> if you don't care about the data and just want the return code to see if something bad happened. But documentation says you should prefer communicate:


Warning: This will deadlock when using stdout=PIPE and/or stderr=PIPE and the child process generates enough output to a pipe such that it blocks waiting for the OS pipe buffer to accept more data. Use communicate() to avoid that.



  • AngularJS - ngBind and Bootstrap Switch
  • python struct.pack(): pack multiple datas in a list or a tuple
  • How to update powerpivot pivot table filter via cell reference?
  • How can I selectively modify the src attributes of script tags in an HTML document using Perl?
  • VBScript to check for open process by user
  • Is a .txt file created in VB different than one I'd randomly create?
  • Rails 4.1 environment variables not reloading
  • Unable to run SDL program in Eclipse but able to do so in Windows Explorer
  • How to create a custom Powershell operator?
  • Basic defensive programming [duplicate]
  • TFS - how do I sum child task hours to parent
  • (Tcl/Expect) clear screen after exit
  • Scanner nextInt() and hasNextInt() problems
  • Write output of for loop to multiple files
  • import scipy.sparse failed
  • Error in installing package: fatal error: stdlib.h: no such file or directory
  • jQuery: How to AJAXify WordPress Search?
  • How to make R's read_csv2() recognise the text characters properly
  • C# fibonacci function returning errors
  • nodemcu custom firmware build problems
  • Android Studio Can't Find tools.jar
  • Default parameter as generic type
  • Trying to get the char code of ENTER key
  • During installation of Django, why do I keep getting ImportError: No module named django?
  • Convert SQLite database to XML
  • where do I find the xml.dom python package for the python-2.6.0-8.9.28 and I have a suse/x86_64 vers
  • Python pickle not one-to-one: different pickles give same object
  • cygwin cannot exec 'git-add--interactive' permission denied
  • Eclipse CDT error: Unable to compile
  • error importing numpy
  • Android - Material Design - NavigationView - How to put vertical scroll?
  • Jquery UI tool tip close icon
  • How to get a value (ex: baseURL) in every Karate feature?
  • SSO with signing and signature validation doesn't work
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?