[Twisted-Python] The right way to change a global variable in a Twisted server?

Christopher Armstrong radix at twistedmatrix.com
Sun Jul 6 08:41:59 EDT 2008

On Sun, Jul 6, 2008 at 3:14 AM, happybrowndog <happybrowndog at hotmail.com> wrote:
> Is there a right way to change a global variable in a Twisted server (this
> one is using PerspectiveBroker)?  What I am trying to do is allow
> perspectivebroker clients to update a global variable on the twisted server
> without colliding, ie: a counter that increases by 1 each time a particular
> call is made to the Twisted server.  Each client has a pb.Avatar attempting
> to update that global variable counter through a method call on the class
> extended on pb.Avatar.  As the server is select-based, am I wrong to assume
> that only one client is executing pb.Avatar calls at a time?

That's correct. Twisted won't run your code in multiple threads at the
same time unless you explicitly ask it to with something like
reactor.callInThread. So updating shared state is safe.

Christopher Armstrong
International Man of Twistery

More information about the Twisted-Python mailing list