hi frank,

please use the mailing list next time...
i don't quite understand the layout of your code: if what you're trying to
achieve is running a server on a separate thread, it's almost a one-liner

threaded_server = ThreadedServer(... any options you want to set ...)
thd = threading.thread(target = threaded_server.start)
thd.start()

now you can close the server at any point of time, using
threaded_server.close()

and if you wish to continue only after it has finished, use
thd.join()

all in all, here's a piece of code that will do all that:

def background_threaded_server(*args, **kwargs):
    ts = ThreadedServer(*args, **kwargs)
    thd = threading.Thread(target = ts.start)
    # monkey-patching the ThreadedServer instance to close() and join()
    orig_close = ts.close
    def close_and_join():
        orig_close()
        thd.join()
    ts.close = close_and_join
    thd.start()
    return ts

by the way, since the threaded server spawns threads for each connection,
you will probably never need more than a single threaded server per process.


hope this helps,
-tomer

An NCO and a Gentleman


On Wed, Jun 8, 2011 at 08:02, Frank Du <[email protected]> wrote:

> Hi Tomer,
>
>    I apologize in advance if this is a trivial question.  I'm currently
> using
>
> the ThreadedServer as defined in rpyc.utils.server
>
> And I have an Thread.target loop that looks like the one provided in rpyc:
>
> def serve_threaded(options):
>    t = ThreadedServer(SlaveService, hostname = options.host,
>        port = options.port, reuse_addr = True,
>        authenticator = options.authenticator, registrar =
> options.registrar,
>        auto_register = options.auto_register)
>    t.logger.quiet = options.quiet
>    if options.logfile:
>        t.logger.console = open(options.logfile, "w")
>    t.start()
>
> When I add this to my own threading object (I've got a module that will
> dynamically start servers wrapping this so it can't be blocking) as so:
>
> def startServer(threaded=True):
>    if threaded:
>        thread = threading.Thread(target=lambda: startServer(app, port,
> threaded=False))
>        thread.setDaemon(True)
>        thread.start()
>        return thread
>
>    # I get some options here similar to the way defined in the standalone
>    # rpyc.server.classic script
>
>    serve_threaded(options)
>
>    So I find there's no way for me to call close() on the ThreadedServer.
> What's the best way for me to have control such that I can kill the server?
>  Do
> I need to extended rpyc.utils.server.Server and clobber its run() command?
> Thanks in advance, rpyc is going to revolutionize my co!
>
> - Frank
>

Reply via email to