Skip to content

Commit

Permalink
Use communicate for model driver
Browse files Browse the repository at this point in the history
  • Loading branch information
langmm committed Sep 19, 2017
1 parent 8acea26 commit fc301e2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 38 deletions.
54 changes: 40 additions & 14 deletions cis_interface/drivers/ModelDriver.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#
# This should not be used directly by modelers
#
from __future__ import print_function
import os
import subprocess
import sys
# import sys
from pprint import pformat
from cis_interface.drivers.Driver import Driver
from cis_interface import backwards
# from cis_interface import backwards


def preexec(): # pragma: no cover
Expand Down Expand Up @@ -63,33 +64,58 @@ def run(self):
try:
self.process = subprocess.Popen(
['stdbuf', '-o0'] + self.args, bufsize=0,
stdin=subprocess.PIPE, stderr=subprocess.STDOUT,
stdin=subprocess.PIPE, stderr=subprocess.PIPE, # STDOUT,
stdout=subprocess.PIPE,
env=self.env, cwd=self.workingDir, preexec_fn=preexec)
except: # pragma: debug
self.exception('(%s): Exception starting in %s with wd %s',
self.args, os.getcwd, self.workingDir)
return

# while True:
# with self.lock:
# if self.process:
# self.process.poll()
# if self.process.returncode is None:
# while True:
# line = self.process.stdout.readline()
# if line:
# print(line, end="")
# else:
# break
# while True:
# line = self.process.stderr.readline()
# if line:
# print(line, end="")
# else:
# break
# else:
# break
# self.sleep()

# Re-direct output
while True:
with self.lock:
if self.process:
line = self.process.stdout.readline()
else:
return
if not line:
break
sys.stdout.write(backwards.bytes2unicode(line))
sys.stdout.flush()
(outdata, errdata) = self.process.communicate()
print(outdata, end="")
print(errdata, end="")
# while True:
# with self.lock:
# if self.process:
# line = self.process.stdout.readline()
# else:
# return
# if not line:
# break
# sys.stdout.write(backwards.bytes2unicode(line))
# sys.stdout.flush()

self.debug(':run: done')

def terminate(self):
r"""Terminate the process running the model."""
self.debug(':terminate()')
with self.lock:
if self.process:
self.process.poll()
if self.process.returncode is None:
self.debug(':terminate(): terminate process')
self.process.terminate()
self.process = None
Expand Down
48 changes: 24 additions & 24 deletions cis_interface/examples/rpcFib/src/rpcFibCli.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,33 @@ int main(int argc, char *argv[]) {
printf("rpcFibCli: yaml has %d lines\n", count_lines(ycontent, "\n") + 1);
free(ycontent);

/* int fib = -1; */
/* int fibNo = -1; */
/* char *logmsg = (char*)malloc(PSI_MSG_MAX*sizeof(char)); */
/* int i; */
/* for (i = 1; i <= iterations; i++) { */
int fib = -1;
int fibNo = -1;
char *logmsg = (char*)malloc(PSI_MSG_MAX*sizeof(char));
int i;
for (i = 1; i <= iterations; i++) {

/* // Call the server and receive response */
/* printf("rpcFibCli(C): fib(->%-2d) ::: ", i); */
/* ret = rpcCall(rpc, i, &fibNo, &fib); */
/* if (ret < 0) { */
/* printf("rpcFibCli(C): RPC CALL ERROR\n"); */
/* free(logmsg); */
/* exit(-1); */
/* } */
// Call the server and receive response
printf("rpcFibCli(C): fib(->%-2d) ::: ", i);
ret = rpcCall(rpc, i, &fibNo, &fib);
if (ret < 0) {
printf("rpcFibCli(C): RPC CALL ERROR\n");
free(logmsg);
exit(-1);
}

/* // Log result by sending it to the log connection */
/* sprintf(logmsg, "fib(%2d<-) = %-2d<-\n", fibNo, fib); */
/* printf("%s", logmsg); */
/* ret = psi_send(log, logmsg, strlen(logmsg)); */
/* if (ret != 0) { */
/* printf("rpcFibCli(C): SEND ERROR\n"); */
/* free(logmsg); */
/* exit(-1); */
/* } */
/* } */
// Log result by sending it to the log connection
sprintf(logmsg, "fib(%2d<-) = %-2d<-\n", fibNo, fib);
printf("%s", logmsg);
ret = psi_send(log, logmsg, strlen(logmsg));
if (ret != 0) {
printf("rpcFibCli(C): SEND ERROR\n");
free(logmsg);
exit(-1);
}
}

/* free(logmsg); */
free(logmsg);
printf("Goodbye from C rpcFibCli\n");
exit(0);

Expand Down

0 comments on commit fc301e2

Please sign in to comment.