[Chris Spencer]
> I'd like to be able to stop a ZEOServer instance from a
> separate thread.

AFAICT, (a) there is no defined API for stopping a ZEO
server; and, (b) there should be.

> I've read ThreadedAsync.LoopCallback, which says to set
> exit_status to a non-none value to stop the server's
> main loop.

That depends on which version of ZODB you're using.  ZODBs
in the 3.2 line have no such gimmick.  Someone working on
Zope3 added this to LoopCallback in a later version of
ZODB, and Zope3 happens to rely on it now.  It's not an
"official" API, and is a hack regardless.

> I've tried doing this, but the server refuses to stop.

Below you misunderstand how Python works; I'll explain.

> Should the following code allow a thread to terminate the
> server.

No, because of the misunderstanding mentioned above.  It's
close ;-)

> Is there a better way than this?

Not that I know of.

> class NewZEOServer(ZEOServer):
>      def loop_forever(self):
>          import ThreadedAsync.LoopCallback
>          ThreadedAsync.LoopCallback.loop(timeout=1.0)
>      def stop(self):
>          from ThreadedAsync.LoopCallback import
>          exit_status = 1

Your stop() method creates a local variable
(`exit_status`), and initializes it to have the same
binding as the module variable named `exit_status` in
module LoopCallback.  Rebinding the local variable named
`exit_status` has no effect on the module variable with the
same name.  What you want instead is, e.g.,

    from ThreadedAsync import LoopCallback
    LoopCallback.exit_status = 1

That changes the binding of the module variable.
For more information about ZODB, see the ZODB Wiki:

ZODB-Dev mailing list  -  ZODB-Dev@zope.org

Reply via email to