Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #96862

Re: Local variables and thread safety

From dieter <dieter@handshake.de>
Subject Re: Local variables and thread safety
Date 2015-09-20 08:02 +0200
References <E6D0DE73-37C9-41BF-89A6-8553D4A1430E@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.31.1442728965.21674.python-list@python.org> (permalink)

Show all headers | View raw


Saint Desmond <st.kooday@gmail.com> writes:

> Dear Team,
> Kindly advise if variables declared within a function are thread safe.

In general, yes. However, there are exceptions.

First of all, "variable" is not the optimal term to use in this context.
In Python, a "variable" is nothing more than the binding of a name
to an object. The local binding in a function itself is thread safe; however,
operations on the bound object may not be thread safe.

For example, if the bound object comes from outside the function
(i.e. it is visible outside the function) then other threads could
modify it and those modifications conflict with each other or
modifications in this thread.

Another example would be the use of a locally defined function
(wich can access "variable"s defined in the enclosing function) as
a thread.


Look at Python's "threading" module to find general purpose utilities
to assure thread safety.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Local variables and thread safety dieter <dieter@handshake.de> - 2015-09-20 08:02 +0200

csiph-web