Skip to content

Instantly share code, notes, and snippets.

@billdawson
Created April 21, 2010 13:11
Show Gist options
  • Save billdawson/373792 to your computer and use it in GitHub Desktop.
Save billdawson/373792 to your computer and use it in GitHub Desktop.
# This emulates what happens in Titanium's android support "run.py"
# script
import subprocess
p = subprocess.Popen(args=['adb', '-e', 'devices'],
stderr=subprocess.PIPE,
stdout=subprocess.PIPE)
# If the adb daemon is NOT already running,
# (i.e., if you run 'adb kill-server' without
# also running 'adb start-server')
# the following line will hang under Windows XP with
# Python 2.6.4 (possibly also with other Windows/Python
# combinations -- this is the only combo i have.)
# It does NOT hang under Linux/OSX.
(so, se) = p.communicate()
print 'Stdout: %s' % so
print 'Stderr: %s' % se
# Discussion: when 'adb -e devices' is run, it will
# start the adb daemon if it's not already running.
# If you do it from the command prompt you see this output:
#
# ----------------------------------------------------
# C:\temp\test_popen>adb -e devices
# * daemon not running. starting it now *
# * daemon started successfully *
# List of devices attached
#
#
# C:\temp\test_popen>
# ----------------------------------------------------
# The fork that occurs to start the adb daemon seems to
# mess around with STDOUT and cause a deadlock if you then
# attempt to read STDOUT on that Popen instance, which
# is exactly what communicate() does. The same deadlock
# occurs if you use .wait() and .stdout.read() instead of
# communicate(), like this:
# >>> p.wait()
# >>> print p.stdout.read() # deadlock
#
#
# Because the adb daemon is _NOT_ running by default on my
# XP machine (I didn't do anything like include 'adb start-server' in a
# startup script), this deadlock _always_ happens the first time I
# launch an app from Titanium Developer in any given XP session.
# Each subsequent launch works, because the adb daemon is then
# successfully started already, and the call to 'adb -e devices'
# does not fork any processes to start the daemon, so STDOUT
# operates normally and no deadlock occurs.
#
# I would recommend checking if platform.system() == 'windows' and, if so, running a Popen
# of 'adb start-server' (without bothering to check stdout -- just fire-and-forget) before
# calling 'adb -e devices'. There doesn't appear be any harm done
# by calling 'adb start-server' even when daemon already running.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment