imports and python locking

Martin Pool mbp at sourcefrog.net
Thu Jul 21 13:37:36 BST 2005


Some benchmarking on bzr's hashcache showed a downside to our current
policy of doing imports in inner scopes.  

Every import statement results in a builtin_import call, which needs to
take and release a lock protecting the module table.  This is done by
semaphores (sem_post, etc) by default.  On Linux, for some reason I
don't understand yet, this always causes a system call to release the
futex, even though I would think it's not necessary.  In any case for
'bzr st' this can take up to 40% of the time.

This is compiled out on python built --without-threads, and it would be
kind of neat to run that for applications that don't use threads, but
that's not really in our scope here.

Practically, I think we should change the practice to generally doing
imports at the top level of the module, except that the top-level code
should only import modules when they're needed for the selected command.

Another approach is to use something like the attached hook to defer
imports.

-- 
Martin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: client.py
Type: text/x-python
Size: 1121 bytes
Desc: not available
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20050721/64289480/attachment.py 


More information about the bazaar mailing list