Catching exception with async=True

25 views
Skip to first unread message

Robert Smith

unread,
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:

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

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
g.py", line 810, in __bootstrap_inner
    self.run()
  File "C:\Users\user\AppData\Local\Continuum\Anaconda\envs\IDE-env\lib\threadin
g.py", 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\__init__.py", 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\__init__.py", line 1282, in run_command_node
    node.cmd.run(input=input, async=async)
  File "C:\Users\user\AppData\Local\Continuum\Anaconda\envs\IDE-env\lib\site-pac
kages\sarge\__init__.py", 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
ss.py", line 710, in __init__
    errread, errwrite)
  File "C:\Users\user\AppData\Local\Continuum\Anaconda\envs\IDE-env\lib\subproce
ss.py", line 958, in _execute_child
    startupinfo)
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.

Thanks

Vinay Sajip

unread,
Oct 5, 2015, 7:05:05 PM10/5/15
to python...@googlegroups.com
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.

Regards,

Vinay Sajip


From: Robert Smith <rsmit...@gmail.com>
To: sarge <python...@googlegroups.com>
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 python-sarge...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Robert Smith

unread,
Oct 6, 2015, 12:59:56 AM10/6/15
to sarge, vinay...@yahoo.co.uk
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.

Regards
Reply all
Reply to author
Forward
0 new messages