Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9128533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:32:13+00:00 2026-06-17T07:32:13+00:00

CherryPy server uses threads to handle requests. One particular method in my threaded server

  • 0

CherryPy server uses threads to handle requests. One particular method in my threaded server is very complex and CPU heavy, so I have to use multiprocessing, from inside the method request thread, to parallelize execution.

I thought I’d just replace

class Server(object)
    @cherrypy.expose
    def expensive_method(self):
        ...
        x = map(fnc, args)
        ...

def fnc(args):
    # this method doesn't need CherryPy but is expensive to compute
    ...

cherrypy.quickstart(Server())

(which works fine) with

    def expensive_method(self):
        pool = Pool()
        x = pool.map(fnc, args)
        pool.terminate()

but that doesn’t work. Even in the simpler case, when I don’t use the pool at all,

    def expensive_method(self):
        pool = Pool()
        x = map(fnc, args) # <== no pool here! same as the working example
        pool.terminate()

I get an exception

[08/Jan/2013:20:05:33] ENGINE Caught signal SIGTERM.
2013-01-08 20:05:33,919 : INFO : _cplogging:201 : error(CP Server Thread-3) : [08/Jan/2013:20:05:33] ENGINE Caught signal SIGTERM.
[08/Jan/2013:20:05:33] ENGINE Bus STOPPING
2013-01-08 20:05:33,920 : INFO : _cplogging:201 : error(CP Server Thread-3) : [08/Jan/2013:20:05:33] ENGINE Bus STOPPING
[08/Jan/2013:20:05:38] ENGINE Error in 'stop' listener <bound method Server.stop of <cherrypy._cpserver.Server object at 0x1090c3c90>>
Traceback (most recent call last):
  File "/Volumes/work/workspace/vew/prj/lib/python2.7/site-packages/cherrypy/process/wspbus.py", line 197, in publish
    output.append(listener(*args, **kwargs))
  File "/Volumes/work/workspace/vew/prj/lib/python2.7/site-packages/cherrypy/process/servers.py", line 223, in stop
    wait_for_free_port(*self.bind_addr)
  File "/Volumes/work/workspace/vew/prj/lib/python2.7/site-packages/cherrypy/process/servers.py", line 410, in wait_for_free_port
    raise IOError("Port %r not free on %r" % (port, host))
IOError: Port 8888 not free on '127.0.0.1'

I think this happens at the end of the request, either after or during pool.terminate().

The forked worker processes don’t do anything with the server or ports as such. Is there any way to tell CherryPy and/or multiprocessing to ignore the “server bits”? I don’t need any ports or sockets there in fnc.

I need this to work on OSX + Linux, using Python 2.7.1 and CherryPy 3.2.2.


PROGRESS 1:

As per Sylvain’s suggestion, I tried pool = Pool(initializer=cherrypy.server.unsubscribe). There are no more exceptions, everything works fine, but in the log I see

[08/Jan/2013:21:16:35] ENGINE Caught signal SIGTERM.
2013-01-08 21:16:35,908 : INFO : _cplogging:201 : error(CP Server Thread-10) : [08/Jan/2013:21:16:35] ENGINE Caught signal SIGTERM.
[08/Jan/2013:21:16:35] ENGINE Bus STOPPING
2013-01-08 21:16:35,909 : INFO : _cplogging:201 : error(CP Server Thread-10) : [08/Jan/2013:21:16:35] ENGINE Bus STOPPING
[08/Jan/2013:21:16:35] ENGINE Bus STOPPED
2013-01-08 21:16:35,909 : INFO : _cplogging:201 : error(CP Server Thread-10) : [08/Jan/2013:21:16:35] ENGINE Bus STOPPED
[08/Jan/2013:21:16:35] ENGINE Bus EXITING
2013-01-08 21:16:35,909 : INFO : _cplogging:201 : error(CP Server Thread-10) : [08/Jan/2013:21:16:35] ENGINE Bus EXITING
[08/Jan/2013:21:16:35] ENGINE Bus EXITED
2013-01-08 21:16:35,910 : INFO : _cplogging:201 : error(CP Server Thread-10) : [08/Jan/2013:21:16:35] ENGINE Bus EXITED

Is that alright? Could any trouble come from this (say, when serving multiple requests in different threads concurrently)?


PROGRESS 2:

Actually the above leaves idle processes behind occasionally 🙁 So it doesn’t work fine. Strange thing is, these idle processes were spawned by Pool, so they ought be daemons, but they actually stay alive even after killing the parent.


PROGRESS 3:

I moved the forking (=Pool() call) outside of the request handling method, but after initializing all the necessary state (so that the worker processes can see this state). No more errors or exceptions.

Bottom line: multiprocessing and multithreading don’t work together.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:32:15+00:00Added an answer on June 17, 2026 at 7:32 am

    Which type of object does ‘self’ refer to? At which point do you initialize and start your forked process? Perhaps a little more code would help diagnose the issue.

    Okay this works just fine:

    import multiprocessing
    import os
    import time
    
    import cherrypy
    
    def run_in_sub_proc(size):
        for i in range(size):
            print os.getpid(), i
            time.sleep(1)
    
    pool = multiprocessing.Pool(2)
    
    class Root(object):
        @cherrypy.expose
        def index(self):
            pool.map_async(run_in_sub_proc, (3, 5))
    
    if __name__ == '__main__':
        cherrypy.engine.subscribe('stop', pool.join)
        cherrypy.quickstart(Root())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a cherrypy web server that uses larges amounts of HTML data. Is
I have an internal cherrypy server that serves static files and answers XMLRPC requests.
I have a cherrypy server distributing xml files to webpages. While my server runs,
I have one server located at example.com running apache, serving my static html files.
I have a python web server (cherrypy), and I want the user to be
It is a rather strange 'bug'. I have written a cherrypy based server. If
Where does the CherryPy server write its error logs to? I have installed CherryPy
quick question: I have created a web server using CherryPy. It requires authentication for
I'm using CherryPy for a web server, but would like it to handle HTTP/1.1
I have the following server: from cherrypy import wsgiserver def my_crazy_app(environ, start_response): status =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.