double import protection - how to ?

H

Helmut Jarausch

Hi,

sorry if this is a FAQ (I couldn't find an answer)

I have a module which gets imported at several different places
not all of which are under my control.

How can I achieve that all/some statements within that module
get executed only at the very first import?
(the statement which must be executed only once, initializes
another OS-thread (java in my case))

Many thanks for a hint,

Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
 
S

Steven D'Aprano

How can I achieve that all/some statements within that module get
executed only at the very first import? (the statement which must be
executed only once, initializes another OS-thread (java in my case))

Python already enforces that. import only executes the module once, the
first time.
 
P

Peter Otten

Helmut said:
I have a module which gets imported at several different places
not all of which are under my control.

How can I achieve that  all/some statements within that module
get executed only at the very first import?

What you describe is Python's default behaviour. A module is executed once
and then cached in sys.modules. The second import is then just a cache
lookup.

This may only fail if a module is imported under different names, typically
when you have directory in sys.path that is part of a package, or when you
import the main script.

Peter
 
H

Helmut Jarausch

Peter said:
What you describe is Python's default behaviour. A module is executed once
and then cached in sys.modules. The second import is then just a cache
lookup.

This may only fail if a module is imported under different names, typically
when you have directory in sys.path that is part of a package, or when you
import the main script.

Peter

Thanks Steven, thanks Peter.

Then it's a problem with a problem with a webserver written in Python (Karrigell-3.0)
and probably related to multi-threading (the statements in my module get definitely
executed more than once).

Thanks for your help,
Helmut.

--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
 
F

Fuzzyman

Thanks Steven, thanks Peter.

Then it's a problem with a problem with a webserver written in Python (Karrigell-3.0)
and probably related to multi-threading (the statements in my module get definitely
executed more than once).

Python has an import lock - so multi-threaded code simultaneously
executing import statements only do the initial import once.

Michael Foord
 
P

Peter Otten

Helmut said:
Thanks Steven, thanks Peter.

Then it's a problem with a problem with a webserver written in Python
(Karrigell-3.0) and probably related to multi-threading (the statements in
my module get definitely executed more than once).

Maybe you have the reload_modules* option switched on? That would defeat
Python's caching in order to ease development.

(*) see http://karrigell.sourceforge.net/en/configuration.htm

Peter
 
H

Helmut Jarausch

Peter said:
Maybe you have the reload_modules* option switched on? That would defeat
Python's caching in order to ease development.

(*) see http://karrigell.sourceforge.net/en/configuration.htm

Thanks Peter for the hint.
Indeed, I am trying to port my application to Karrigell-3.0,
where Python's caching is bypassed and the statements in the
module get executed each time. So, I have to install a trap door
using a global variable.

Helmut.


--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
DewittMill
Top