Skip to content

Instantly share code, notes, and snippets.

@namoopsoo
Created May 29, 2014 16:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save namoopsoo/104dd7c568df17255bb5 to your computer and use it in GitHub Desktop.
Save namoopsoo/104dd7c568df17255bb5 to your computer and use it in GitHub Desktop.
Here is a straightforward way to capture the standard and error outputs from a custom process you are running. Back-linking to http://stackoverflow.com/a/23937468/472876
from multiprocessing import Process
import os
import sys
class MyProc(Process):
def run(self):
# Define the logging in run(), MyProc's entry function when it is .start()-ed
# p = MyProc()
# p.start()
self.initialize_logging()
print 'Now output is captured.'
# Now do stuff...
def initialize_logging(self):
sys.stdout = open(str(os.getpid()) + ".out", "a", buffering=0)
sys.stderr = open(str(os.getpid()) + "_error.out", "a", buffering=0)
print 'stdout initialized'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment