Catching exception with async=True

Skip to first unread message

Robert Smith

Oct 5, 2015, 1:19:12 PM10/5/15
to sarge
How can I catch an exception when using async=True? This doesn't seem to work (in Windows) when there is not a bash executable available:

    p ='bash -c "pwd", async=True, stdout=sarge.Capture(), stderr=sarge.Capture())
except Exception as e:

The traceback is the following:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Continuum\Anaconda\envs\IDE-env\lib\threadin", line 810, in __bootstrap_inner
  File "C:\Users\user\AppData\Local\Continuum\Anaconda\envs\IDE-env\lib\threadin", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\Users\user\AppData\Local\Continuum\Anaconda\envs\IDE-env\lib\site-pac
kages\sarge\", line 1136, in run_node
    result = getattr(self, method)(node, input, async)
  File "C:\Users\user\AppData\Local\Continuum\Anaconda\envs\IDE-env\lib\site-pac
kages\sarge\", line 1282, in run_command_node, async=async)
  File "C:\Users\user\AppData\Local\Continuum\Anaconda\envs\IDE-env\lib\site-pac
kages\sarge\", line 632, in run
    self.process = p = Popen(self.args, **self.kwargs)
  File "C:\Users\user\AppData\Local\Continuum\Anaconda\envs\IDE-env\lib\subproce", line 710, in __init__
    errread, errwrite)
  File "C:\Users\user\AppData\Local\Continuum\Anaconda\envs\IDE-env\lib\subproce", line 958, in _execute_child
WindowsError: [Error 2] The system cannot find the file specified

Something similar happens in Linux when trying to use a wrong command instead of bash.


Vinay Sajip

Oct 5, 2015, 7:05:05 PM10/5/15
You can't really do this, since async=True runs the command on a background thread, so you can't catch exceptions raised there. You can, however, spin up your own thread and in there call run with async=False - you should then be able to catch any exceptions.


Vinay Sajip

From: Robert Smith <>
To: sarge <>
Sent: Monday, 5 October 2015, 18:19
Subject: Catching exception with async=True

You received this message because you are subscribed to the Google Groups "sarge" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
For more options, visit

Robert Smith

Oct 6, 2015, 12:59:56 AM10/6/15
to sarge,
Thank you for your response. Using another thread is a bit inconvenient because I'm already using other threads. Maybe I need another approach to circumvent this limitation.

Reply all
Reply to author
0 new messages