Starting CherryPy from Java results in not responding answers

223 views Asked by At

I have a Python-Project for which I created a CherryPy-Webservice-Wrapper to access it via a Java-Client (a self developed Eclipse plugin in particular). The CherryPy-Server is also started from Java as an external process:

Runtime.getRuntime().exec("python C:/Coding/Projects/p1/CherryPyServer.py " + port);

To operate the CherryPy-Server from the Java-Client, I use this:

new URL("http://localhost:" + port + "/" + conf).openStream();

For testing I also access CherryPy with my Browser (Google Chrome). But after a while, with a new request CherryPy doesn't react anymore, in Java and also in Chrome (Chrome shows a slowly left-spinning wheel), meaning the client doesn't get an answer from CherryPy. This happens unreproducible, so I never can say when it happens. Also this only happens when I start CherryPy as an external process from Java. When I start CherryPy as a "normal" service, this problem doesn't occur.

My CherryPy-Python-Project has many stdout- and stderr-outputs, maybe it has something to do with that?

Note: Switching to Jython is not an option (because I need the c_types-library)

2

There are 2 answers

0
Isa Hassen On

In addition to jwalker's comment about the STDOUT buffer - maybe try piping STDOUT and STDERROR to /dev/null, which in newer versions of bash would be:

python mycherrypyserver.py &> /dev/null

I would also recommend taking a look at the your CherryPy sessions, if you have it enabled. Session locking may prevent the same client from being able to view another request. See here: http://blog.schmichael.com/2007/09/20/session-locking-and-performance-in-cherrypy/

0
saaj On

To avoid possible effect of what @jwalker has pointed to, you need to stop console logging (log.screen) which is only useful while you're developing. To achieve this you can set apt CherryPy environment, like cherrypy.config.update({'environment': 'production'}). It always better use your tools, not fight them like @Isa suggested.

When you see "left-spinning wheel", it indicates that CherryPy has received your request but can't handle it at the moment. It maybe a session lock, if you use sessions and send concurrent requests, like @Isa wrote. But it also may be the case that your Python app you proxying with CherryPy, blocks, and because CherryPy is a threaded-server you run out of available worker-threads.

Whatever reason is, you can leverage logging to understand your problem. Look at access and error logs for correspondence to your requests. Log an entry when your Python app starts and ends processing a request. You can also do it at CherryPy side, like:

#!/usr/bin/env python
# -*- coding: utf-8 -*-


import time

import cherrypy


config = {
  'global' : {
    'server.socket_host' : '127.0.0.1',
    'server.socket_port' : 8080,
    'server.thread_pool' : 8,

    'log.access_file' : 'access.log',
    'log.error_file'  : 'error.log',
  }
}


def logStartFinish(fn):

  def wrap(*args, **kwargs):
    cherrypy.log('Start')
    try:
      return fn(*args, **kwargs)
    finally:
      cherrypy.log('Finish')

  return wrap


class App:

  @cherrypy.expose
  @logStartFinish
  def index(self):
    time.sleep(4) # Call to Python app you wrap
    return 'Some result'


if __name__ == '__main__':
  cherrypy.config.update({'environment': 'production'})
  cherrypy.quickstart(App(), '/', config)