Search code examples
pythoncherrypy

How is CherryPy providing the appropriate request context?


I'm a bit new to Python and I'm having trouble understanding some code from this answer:

https://stackoverflow.com/a/3753314/52551

The code is:

@cherrypy.expose
def update(self):
    cl = cherrypy.request.headers['Content-Length']
    rawbody = cherrypy.request.body.read(int(cl))
    body = simplejson.loads(rawbody)
    # do_something_with(body)
    return "Updated %r." % (body,)

Like I said I'm a bit new at Python so my confusion is how cherrypy.request is able to provide the appropriate request context. If two clients make a request couldn't the first client's request information be overwritten by the second client's request information if there is a context switch somewhere inside the update method?


Solution

  • CherryPy uses a threading.local object to manage the request and response context.