Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 172c40b

Browse files
committed
Whitespace normalization.
1 parent dfc538a commit 172c40b

3 files changed

Lines changed: 187 additions & 187 deletions

File tree

Demo/threads/bug.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# The following self-contained little program usually freezes with most
22
# threads reporting
3-
#
3+
#
44
# Unhandled exception in thread:
55
# Traceback (innermost last):
66
# File "importbug.py", line 6
77
# x = whrandom.randint(1,3)
88
# AttributeError: randint
9-
#
9+
#
1010
# Here's the program; it doesn't use anything from the attached module:
1111

1212
import thread
@@ -33,37 +33,37 @@ def task():
3333

3434
# Sticking an acquire/release pair around the 'import' statement makes the
3535
# problem go away.
36-
#
36+
#
3737
# I believe that what happens is:
38-
#
38+
#
3939
# 1) The first thread to hit the import atomically reaches, and executes
4040
# most of, get_module. In particular, it finds Lib/whrandom.pyc,
4141
# installs its name in sys.modules, and executes
42-
#
42+
#
4343
# v = eval_code(co, d, d, d, (object *)NULL);
44-
#
44+
#
4545
# to initialize the module.
46-
#
46+
#
4747
# 2) eval_code "ticker"-slices the 1st thread out, and gives another thread
4848
# a chance. When this 2nd thread hits the same 'import', import_module
4949
# finds 'whrandom' in sys.modules, so just proceeds.
50-
#
50+
#
5151
# 3) But the 1st thread is still "in the middle" of executing whrandom.pyc.
5252
# So the 2nd thread has a good chance of trying to look up 'randint'
5353
# before the 1st thread has placed it in whrandom's dict.
54-
#
54+
#
5555
# 4) The more threads there are, the more likely that at least one of them
5656
# will do this before the 1st thread finishes the import work.
57-
#
57+
#
5858
# If that's right, a perhaps not-too-bad workaround would be to introduce a
5959
# static "you can't interrupt this thread" flag in ceval.c, check it before
6060
# giving up interpreter_lock, and have IMPORT_NAME set it & restore (plain
6161
# clearing would not work) it around its call to import_module. To its
6262
# credit, there's something wonderfully perverse about fixing a race via an
6363
# unprotected static <grin>.
64-
#
64+
#
6565
# as-with-most-other-things-(pseudo-)parallel-programming's-more-fun-
6666
# in-python-too!-ly y'rs - tim
67-
#
67+
#
6868
# Tim Peters [email protected]
6969
# not speaking for Kendall Square Research Corp

Demo/threads/find.py

Lines changed: 96 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -32,121 +32,121 @@
3232

3333
class WorkQ:
3434

35-
# Invariants:
36-
37-
# - busy and work are only modified when mutex is locked
38-
# - len(work) is the number of jobs ready to be taken
39-
# - busy is the number of jobs being done
40-
# - todo is locked iff there is no work and somebody is busy
41-
42-
def __init__(self):
43-
self.mutex = thread.allocate()
44-
self.todo = thread.allocate()
45-
self.todo.acquire()
46-
self.work = []
47-
self.busy = 0
48-
49-
def addwork(self, func, args):
50-
job = (func, args)
51-
self.mutex.acquire()
52-
self.work.append(job)
53-
self.mutex.release()
54-
if len(self.work) == 1:
55-
self.todo.release()
56-
57-
def _getwork(self):
58-
self.todo.acquire()
59-
self.mutex.acquire()
60-
if self.busy == 0 and len(self.work) == 0:
61-
self.mutex.release()
62-
self.todo.release()
63-
return None
64-
job = self.work[0]
65-
del self.work[0]
66-
self.busy = self.busy + 1
67-
self.mutex.release()
68-
if len(self.work) > 0:
69-
self.todo.release()
70-
return job
71-
72-
def _donework(self):
73-
self.mutex.acquire()
74-
self.busy = self.busy - 1
75-
if self.busy == 0 and len(self.work) == 0:
76-
self.todo.release()
77-
self.mutex.release()
78-
79-
def _worker(self):
80-
time.sleep(0.00001) # Let other threads run
81-
while 1:
82-
job = self._getwork()
83-
if not job:
84-
break
85-
func, args = job
86-
apply(func, args)
87-
self._donework()
88-
89-
def run(self, nworkers):
90-
if not self.work:
91-
return # Nothing to do
92-
for i in range(nworkers-1):
93-
thread.start_new(self._worker, ())
94-
self._worker()
95-
self.todo.acquire()
35+
# Invariants:
36+
37+
# - busy and work are only modified when mutex is locked
38+
# - len(work) is the number of jobs ready to be taken
39+
# - busy is the number of jobs being done
40+
# - todo is locked iff there is no work and somebody is busy
41+
42+
def __init__(self):
43+
self.mutex = thread.allocate()
44+
self.todo = thread.allocate()
45+
self.todo.acquire()
46+
self.work = []
47+
self.busy = 0
48+
49+
def addwork(self, func, args):
50+
job = (func, args)
51+
self.mutex.acquire()
52+
self.work.append(job)
53+
self.mutex.release()
54+
if len(self.work) == 1:
55+
self.todo.release()
56+
57+
def _getwork(self):
58+
self.todo.acquire()
59+
self.mutex.acquire()
60+
if self.busy == 0 and len(self.work) == 0:
61+
self.mutex.release()
62+
self.todo.release()
63+
return None
64+
job = self.work[0]
65+
del self.work[0]
66+
self.busy = self.busy + 1
67+
self.mutex.release()
68+
if len(self.work) > 0:
69+
self.todo.release()
70+
return job
71+
72+
def _donework(self):
73+
self.mutex.acquire()
74+
self.busy = self.busy - 1
75+
if self.busy == 0 and len(self.work) == 0:
76+
self.todo.release()
77+
self.mutex.release()
78+
79+
def _worker(self):
80+
time.sleep(0.00001) # Let other threads run
81+
while 1:
82+
job = self._getwork()
83+
if not job:
84+
break
85+
func, args = job
86+
apply(func, args)
87+
self._donework()
88+
89+
def run(self, nworkers):
90+
if not self.work:
91+
return # Nothing to do
92+
for i in range(nworkers-1):
93+
thread.start_new(self._worker, ())
94+
self._worker()
95+
self.todo.acquire()
9696

9797

9898
# Main program
9999

100100
def main():
101-
sys.argv.append("/tmp")
102-
nworkers = 4
103-
opts, args = getopt.getopt(sys.argv[1:], '-w:')
104-
for opt, arg in opts:
105-
if opt == '-w':
106-
nworkers = string.atoi(arg)
107-
if not args:
108-
args = [os.curdir]
101+
sys.argv.append("/tmp")
102+
nworkers = 4
103+
opts, args = getopt.getopt(sys.argv[1:], '-w:')
104+
for opt, arg in opts:
105+
if opt == '-w':
106+
nworkers = string.atoi(arg)
107+
if not args:
108+
args = [os.curdir]
109109

110-
wq = WorkQ()
111-
for dir in args:
112-
wq.addwork(find, (dir, selector, wq))
110+
wq = WorkQ()
111+
for dir in args:
112+
wq.addwork(find, (dir, selector, wq))
113113

114-
t1 = time.time()
115-
wq.run(nworkers)
116-
t2 = time.time()
114+
t1 = time.time()
115+
wq.run(nworkers)
116+
t2 = time.time()
117117

118-
sys.stderr.write('Total time ' + `t2-t1` + ' sec.\n')
118+
sys.stderr.write('Total time ' + `t2-t1` + ' sec.\n')
119119

120120

121121
# The predicate -- defines what files we look for.
122122
# Feel free to change this to suit your purpose
123123

124124
def selector(dir, name, fullname, stat):
125-
# Look for group or world writable files
126-
return (stat[ST_MODE] & 0022) != 0
125+
# Look for group or world writable files
126+
return (stat[ST_MODE] & 0022) != 0
127127

128128

129129
# The find procedure -- calls wq.addwork() for subdirectories
130130

131131
def find(dir, pred, wq):
132-
try:
133-
names = os.listdir(dir)
134-
except os.error, msg:
135-
print `dir`, ':', msg
136-
return
137-
for name in names:
138-
if name not in (os.curdir, os.pardir):
139-
fullname = os.path.join(dir, name)
140-
try:
141-
stat = os.lstat(fullname)
142-
except os.error, msg:
143-
print `fullname`, ':', msg
144-
continue
145-
if pred(dir, name, fullname, stat):
146-
print fullname
147-
if S_ISDIR(stat[ST_MODE]):
148-
if not os.path.ismount(fullname):
149-
wq.addwork(find, (fullname, pred, wq))
132+
try:
133+
names = os.listdir(dir)
134+
except os.error, msg:
135+
print `dir`, ':', msg
136+
return
137+
for name in names:
138+
if name not in (os.curdir, os.pardir):
139+
fullname = os.path.join(dir, name)
140+
try:
141+
stat = os.lstat(fullname)
142+
except os.error, msg:
143+
print `fullname`, ':', msg
144+
continue
145+
if pred(dir, name, fullname, stat):
146+
print fullname
147+
if S_ISDIR(stat[ST_MODE]):
148+
if not os.path.ismount(fullname):
149+
wq.addwork(find, (fullname, pred, wq))
150150

151151

152152
# Call the main program

0 commit comments

Comments
 (0)