bug.py :  » Language-Interface » ChinesePython » chinesepython2.1.3-0.4 » Demo » threads » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » ChinesePython 
ChinesePython » chinesepython2.1.3 0.4 » Demo » threads » bug.py
# The following self-contained little program usually freezes with most
# threads reporting
#
# Unhandled exception in thread:
# Traceback (innermost last):
#   File "importbug.py", line 6
#     x = whrandom.randint(1,3)
# AttributeError: randint
#
# Here's the program; it doesn't use anything from the attached module:

import thread

def task():
    global N
    import whrandom
    x = whrandom.randint(1,3)
    a.acquire()
    N = N - 1
    if N == 0: done.release()
    a.release()

a = thread.allocate_lock()
done = thread.allocate_lock()
N = 10

done.acquire()
for i in range(N):
    thread.start_new_thread(task, ())
done.acquire()
print 'done'


# Sticking an acquire/release pair around the 'import' statement makes the
# problem go away.
#
# I believe that what happens is:
#
# 1) The first thread to hit the import atomically reaches, and executes
#    most of, get_module.  In particular, it finds Lib/whrandom.pyc,
#    installs its name in sys.modules, and executes
#
#         v = eval_code(co, d, d, d, (object *)NULL);
#
#    to initialize the module.
#
# 2) eval_code "ticker"-slices the 1st thread out, and gives another thread
#    a chance.  When this 2nd thread hits the same 'import', import_module
#    finds 'whrandom' in sys.modules, so just proceeds.
#
# 3) But the 1st thread is still "in the middle" of executing whrandom.pyc.
#    So the 2nd thread has a good chance of trying to look up 'randint'
#    before the 1st thread has placed it in whrandom's dict.
#
# 4) The more threads there are, the more likely that at least one of them
#    will do this before the 1st thread finishes the import work.
#
# If that's right, a perhaps not-too-bad workaround would be to introduce a
# static "you can't interrupt this thread" flag in ceval.c, check it before
# giving up interpreter_lock, and have IMPORT_NAME set it & restore (plain
# clearing would not work) it around its call to import_module.  To its
# credit, there's something wonderfully perverse about fixing a race via an
# unprotected static <grin>.
#
# as-with-most-other-things-(pseudo-)parallel-programming's-more-fun-
#    in-python-too!-ly y'rs  - tim
#
# Tim Peters   tim@ksr.com
# not speaking for Kendall Square Research Corp
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.