sjs7007 / S3Lab

Code for Deep Cloud back end.
http://deepc05.acis.ufl.edu:8080/
0 stars 0 forks source link

Allow user to pause and resume. #5

Closed sjs7007 closed 8 years ago

sjs7007 commented 8 years ago

The pause and resumption can be based on saving neural network weights and resuming from there or actually pausing the script.

First working on pausing the script and resuming.

Stackoverflow : http://stackoverflow.com/questions/7180914/pause-resume-a-python-script-in-middle

Seems cont and stop from kill can be used : http://www.pervasivecode.com/blog/2010/04/03/unix-tip-kill-stop-and-kill-cont/comment-page-1/

Try on python script : works and then from nodejs process.kill() : works, syntax is proces.kill(,'SIGSTOP') to stop

sigstop sends stop signal, similarly sigcont sends continue signal.

signals :

http://linux.die.net/man/7/signal https://nodejs.org/api/process.html#process_process_kill_pid_signal

and then the #4 example.

sjs7007 commented 8 years ago

It seems using SIGSTP would be better as SIGSTOP can't be caught in python

http://www.gossamer-threads.com/lists/python/python/953193

def handler(signalnum, stackframe): 
print "Received signal %d" % signalnum 
os.kill(os.getpid(), signal.SIGSTOP) # Hit myself with a brick. 

signal.signal(signal.SIGTSTP, handler) 

works

sjs7007 commented 8 years ago

Tried pause and continue on browser also, works.

Sample Client Code:

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.8/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost:9090');
  socket.on('pythonSocket',function( data ) {
    document.write(data+"<br>");
  });
  socket.emit('pause');
  socket.emit('continue');
</script>

Sample Server Code :

var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var count = 0;

server.listen(9090);

app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});

io.on('connection', function (socket) {
    console.log("here 2");
    var spawn = require('child_process').spawn;
    var py = spawn('python',['test.py']);

    py.stdout.on('data',function(pyStdout) {
        console.log("godyt data count : "+count++)
        console.log(pyStdout.toString());
        socket.emit('pythonSocket',pyStdout.toString());
    });

    //to stop process 
    socket.on("pause",function() {
        console.log("pausing pid : "+py.pid);
        socket.emit("pythonSocket","pausing pid : "+py.pid);
        process.kill(py.pid,"SIGTSTP");
    });

    //to resume process
    socket.on("continue",function() {
        console.log("resuming pid : "+py.pid);
        socket.emit("pythonSocket","resuming pid : "+py.pid);
        process.kill(py.pid,"SIGCONT");
    }); 

});