473,325 Members | 2,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

import, functions, and threading

There were some posts recently discussing whether it's poor style to put
import statements inside functions. I recently got reminded that there's
one very good reason to avoid it: Python has an import lock that blocks
more than one thread from executing import statements. Putting import
statements inside functions that might be called in a threaded
environment is asking for deadlock trouble.

--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"Do not taunt happy fun for loops. Do not change lists you are looping over."
--Remco Gerlich, comp.lang.python
Jul 18 '05 #1
2 1481
aa**@pythoncraft.com (Aahz) wrote in message news:<c2**********@panix2.panix.com>...
There were some posts recently discussing whether it's poor style to put
import statements inside functions. I recently got reminded that there's
one very good reason to avoid it: Python has an import lock that blocks
more than one thread from executing import statements. Putting import
statements inside functions that might be called in a threaded
environment is asking for deadlock trouble.


Just saw this on the week Python URL... anyway, an alternative that
should be safer:

somemodule = None

def whatever():
global somemodule
if somemodule is None:
import somemodule

I'm still not sure if it's entirely safe in all cases, but I think it
is...? Anyway, it allows for lazy loading.
Jul 18 '05 #2
In article <35**************************@posting.google.com >,
Ian Bicking <ia**@colorstudy.com> wrote:
aa**@pythoncraft.com (Aahz) wrote in message news:<c2**********@panix2.panix.com>...

There were some posts recently discussing whether it's poor style to put
import statements inside functions. I recently got reminded that there's
one very good reason to avoid it: Python has an import lock that blocks
more than one thread from executing import statements. Putting import
statements inside functions that might be called in a threaded
environment is asking for deadlock trouble.


Just saw this on the week Python URL... anyway, an alternative that
should be safer:

somemodule = None

def whatever():
global somemodule
if somemodule is None:
import somemodule

I'm still not sure if it's entirely safe in all cases, but I think it
is...? Anyway, it allows for lazy loading.


Enh. You've got an idea, but your implementation probably still has a
race condition. And you're breaking "simple is better than complex".
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"usenet imitates usenet" --Darkhawk
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: MackS | last post by:
Hi I'm new to Python, I've read the FAQ but still can't get the following simple example working: # file main_mod.py: global_string = 'abc' def main():
2
by: Tian | last post by:
I am writing a python program which needs to support some plug-ins. I have an XML file storing some dynamic structures. XML file records some class names whose instance needs to be created in the...
24
by: Gorlon the Impossible | last post by:
Hello I'm not sure how to phrase this question. I have a Python function that sends MIDI messages to a synth. When I run it, I of course have to wait until it is finished before I can do anything...
1
by: Nick Coghlan | last post by:
Several standard library modules (e.g., cPickle/pickle, cStringIO/StringIO, threading/dummy_threading) have versions which may not be available on all platforms, and pure Python fallbacks that work...
3
by: Mudcat | last post by:
I have a directory structure that contains different modules that run depending on what the user selects. They are identical in name and structure, but what varies is the content of the functions....
2
by: Panard | last post by:
Hi, I'm experiencing a strange problem while trying to manage a ftp connection into a separate thread. I'm on linux, python 2.4.3 Here is a test : ------ ftp_thread.py ------ import ftplib...
4
by: ahagley | last post by:
The problem: 1) I'm not a programmer, most of the time I'm a physicist, but I do numerical simulation. 2) I have a DLL that I want to use functions from. I have the .h file associated with the...
3
by: Rakesh Kumar | last post by:
Hi - I am actually trying to get my feet in multi-threaded C++ programming. While I am aware that the C++ standard does not talk about threads (at least, for now - in C++03) - my question is more...
9
by: rsoh.woodhouse | last post by:
Hi, I'm trying to work out some strange (to me) behaviour that I see when running a python script in two different ways (I've inherited some code that needs to be maintained and integrated with...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.