Skip to content

Instantly share code, notes, and snippets.

@dstuebe
Last active December 20, 2015 03:39
Show Gist options
  • Save dstuebe/6065487 to your computer and use it in GitHub Desktop.
Save dstuebe/6065487 to your computer and use it in GitHub Desktop.
Demo python program that writes stuff to a stream handler and a file handler. Hoping to show failure when executed using Java ProcessBuilder with output piped to Java buffer. Very strange behavior - the python process just hangs as though it is waiting on the stream when run on windows.
from mpi4py import MPI
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
if rank == 0:
data = {'key1' : [7, 2.72, 2+3j],
'key2' : ( 'abc', 'xyz')}
else:
data = None
data = comm.bcast(data, root=0)
print 'Hello from %s of %s: data: %s' % (rank, size, data)
#!/usr/bin/env python
import logging
import string
import random
# Default logger goes to stderr, print goes to stdout...
def main(nlog, flog=False, clog=False):
# create logger with 'spam_application'
logger = logging.getLogger('logger_test')
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# create file handler with a debug log level
if flog:
fh = logging.FileHandler('logger_test.log', mode='w')
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
logger.addHandler(fh)
# create console handler with a higher log level
if clog:
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
ch.setFormatter(formatter)
logger.addHandler(ch)
logger.info('Hello World')
log_funcs = [
logger.info,
logger.debug,
logger.critical,
]
for cnt in xrange(nlog):
func_index = random.randint(0,len(log_funcs)-1)
log_funcs[func_index](''.join(random.choice(string.letters) for i in xrange(random.randint(10, 100))))
print ''.join(random.choice(string.letters) for i in xrange(random.randint(10, 100)))
logger.info('Goodbye World!')
exit(0)
if __name__ == '__main__':
main(5000, flog=True, clog=True)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class TestProcess {
public static void main(String[] args) {
try {
System.out.println("Creating Process");
int exitvalue = createProcess();
System.out.println("Process Complete:" + exitvalue);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static int createProcess() throws IOException, InterruptedException{
ArrayList<String> processCommands = new ArrayList<String>();
processCommands.add("/Users/dstuebe/Documents/Dev/code/pyja_logger/logger.py");
//processCommands.add("/Users/dstuebe/Documents/Dev/code/pyja_logger/printer.py");
//processCommands.add("sleep");
//processCommands.add("5");
for(String str : processCommands)
System.out.println(str+"--");
ProcessBuilder processBuilder = new ProcessBuilder(processCommands);
Process process = processBuilder.start();
final InputStream is = process.getInputStream();
final InputStream es = process.getErrorStream();
class RunnableStream implements Runnable{
InputStream myStream;
String myname;
public RunnableStream(InputStream s, String name){
myStream = s;
myname = name;
}
public void run() {
try {
BufferedReader reader =
new BufferedReader(new InputStreamReader(myStream));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(myname+line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
myStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// the background thread watches the output from the process
Thread istreamThread = new Thread(new RunnableStream(is, "I: "));
Thread estreamThread = new Thread(new RunnableStream(es, "E: "));
estreamThread.start();
istreamThread.start();
int exitValue = process.waitFor();
estreamThread.join(0);
istreamThread.join(0);
return exitValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment