[BangPypers] Python script hangs after using logging module
Noufal Ibrahim KV
noufal at nibrahim.net.in
Tue May 27 07:18:33 CEST 2014
On Tue, May 27 2014, Rahul Gopan wrote:
> #!/usr/local/bin/python2.7
> import commands
> import subprocess
> items = commands.getoutput('COMMAND <branch> <Begin_no> <End_no> | wc -l')
> print items
>
> -- Works --
>
> #!/usr/local/bin/python2.7
> import commands
> import subprocess
> import logging
> logging.basicConfig(filename='Log_file.log',filemode='w',format='%(asctime)s
> %(message)s',level=logging.DEBUG)
> items = commands.getoutput('COMMAND <branch> <Begin_no> <End_no> | wc -l')
> print items
>
> -- Hangs --
A few suggestions.
1. Consider using subprocess or envoy[1] instead of commands. It's
deprecated.
2. Please specify exactly what you're trying to run rather than the
placeholder you've put in your script above. My guess is that it's
waiting for input and holding up the pipeline so that wc is never
run.
3. Try running that command outside to see what the behaviour is.
4. If the command works, make sure that your script is running in the
same directory and with the same environment as the one where the
command works.
5. I'd be inclined to do the `wc` in Python rather than use a pipe. It
looks okay in this case but usually, involving the shell while trying
to execute something leads to headaches with quoting, variable expansion
and other such things that are not worth it.
6. The basicConfig line will actually create and open the Log_file.log
file. If you're on a disk that's misbehaving (e.g. a stale NFS
mount), it might hang. Try creating the file manually outside the
script to see if that's the issue.
[...]
Footnotes:
[1] https://github.com/kennethreitz/envoy
--
Cordially,
Noufal
http://nibrahim.net.in
More information about the BangPypers
mailing list