summaryrefslogtreecommitdiffstats
path: root/Demo/threads
diff options
context:
space:
mode:
Diffstat (limited to 'Demo/threads')
-rw-r--r--Demo/threads/Coroutine.py14
-rw-r--r--Demo/threads/Generator.py18
-rw-r--r--Demo/threads/fcmp.py26
-rw-r--r--Demo/threads/find.py8
-rw-r--r--Demo/threads/squasher.py4
-rw-r--r--Demo/threads/sync.py52
-rw-r--r--Demo/threads/telnet.py14
7 files changed, 66 insertions, 70 deletions
diff --git a/Demo/threads/Coroutine.py b/Demo/threads/Coroutine.py
index 10fa303..6bf788b 100644
--- a/Demo/threads/Coroutine.py
+++ b/Demo/threads/Coroutine.py
@@ -78,7 +78,7 @@ class _CoEvent:
if self.f is None:
return 'main coroutine'
else:
- return 'coroutine for func ' + self.f.func_name
+ return 'coroutine for func ' + self.f.__name__
def __hash__(self):
return id(self)
@@ -125,9 +125,9 @@ class Coroutine:
def kill(self):
if self.killed:
- raise TypeError, 'kill() called on dead coroutines'
+ raise TypeError('kill() called on dead coroutines')
self.killed = 1
- for coroutine in self.invokedby.keys():
+ for coroutine in list(self.invokedby.keys()):
coroutine.resume()
def back(self, data=None):
@@ -137,10 +137,10 @@ class Coroutine:
return self.tran( self.main, data )
def tran(self, target, data=None):
- if not self.invokedby.has_key(target):
- raise TypeError, '.tran target %r is not an active coroutine' % (target,)
+ if target not in self.invokedby:
+ raise TypeError('.tran target %r is not an active coroutine' % (target,))
if self.killed:
- raise TypeError, '.tran target %r is killed' % (target,)
+ raise TypeError('.tran target %r is killed' % (target,))
self.value = data
me = self.active
self.invokedby[target] = me
@@ -152,7 +152,7 @@ class Coroutine:
if self.main is not me:
raise Killed
if self.terminated_by is not None:
- raise EarlyExit, '%r terminated early' % (self.terminated_by,)
+ raise EarlyExit('%r terminated early' % (self.terminated_by,))
return self.value
diff --git a/Demo/threads/Generator.py b/Demo/threads/Generator.py
index 63bed9b..0cc1bda 100644
--- a/Demo/threads/Generator.py
+++ b/Demo/threads/Generator.py
@@ -32,7 +32,7 @@ class Generator:
# Called by producer for each value; raise Killed if no more needed
def put(self, value):
if self.killed:
- raise TypeError, 'put() called on killed generator'
+ raise TypeError('put() called on killed generator')
self.value = value
self.getlock.release() # Resume consumer thread
self.putlock.acquire() # Wait for next get() call
@@ -41,7 +41,7 @@ class Generator:
# Called by producer to get next value; raise EOFError if no more
def get(self):
if self.killed:
- raise TypeError, 'get() called on killed generator'
+ raise TypeError('get() called on killed generator')
self.putlock.release() # Resume producer thread
self.getlock.acquire() # Wait for value to appear
if self.done:
@@ -50,7 +50,7 @@ class Generator:
# Called by consumer if no more values wanted
def kill(self):
if self.killed:
- raise TypeError, 'kill() called on killed generator'
+ raise TypeError('kill() called on killed generator')
self.killed = 1
self.putlock.release()
# Clone constructor
@@ -58,27 +58,27 @@ class Generator:
return Generator(self.func, self.args)
def pi(g):
- k, a, b, a1, b1 = 2L, 4L, 1L, 12L, 4L
+ k, a, b, a1, b1 = 2, 4, 1, 12, 4
while 1:
# Next approximation
- p, q, k = k*k, 2L*k+1L, k+1L
+ p, q, k = k*k, 2*k+1, k+1
a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
# Print common digits
d, d1 = a/b, a1/b1
while d == d1:
g.put(int(d))
- a, a1 = 10L*(a%b), 10L*(a1%b1)
+ a, a1 = 10*(a%b), 10*(a1%b1)
d, d1 = a/b, a1/b1
def test():
g = Generator(pi, ())
g.kill()
g = Generator(pi, ())
- for i in range(10): print g.get(),
- print
+ for i in range(10): print(g.get(), end=' ')
+ print()
h = g.clone()
g.kill()
while 1:
- print h.get(),
+ print(h.get(), end=' ')
test()
diff --git a/Demo/threads/fcmp.py b/Demo/threads/fcmp.py
index 27af76d..bc2e3ed 100644
--- a/Demo/threads/fcmp.py
+++ b/Demo/threads/fcmp.py
@@ -16,10 +16,10 @@ def printinorder(list):
f = co.create(fringe, co, list)
try:
while 1:
- print co.tran(f),
+ print(co.tran(f), end=' ')
except EarlyExit:
pass
- print
+ print()
printinorder([1,2,3]) # 1 2 3
printinorder([[[[1,[2]]],3]]) # ditto
@@ -49,16 +49,16 @@ def fcmp(l1, l2):
co1.kill(); co2.kill()
return cmp(v1,v2)
-print fcmp(range(7), x) # 0; fringes are equal
-print fcmp(range(6), x) # -1; 1st list ends early
-print fcmp(x, range(6)) # 1; 2nd list ends early
-print fcmp(range(8), x) # 1; 2nd list ends early
-print fcmp(x, range(8)) # -1; 1st list ends early
-print fcmp([1,[[2],8]],
- [[[1],2],8]) # 0
-print fcmp([1,[[3],8]],
- [[[1],2],8]) # 1
-print fcmp([1,[[2],8]],
- [[[1],2],9]) # -1
+print(fcmp(range(7), x)) # 0; fringes are equal
+print(fcmp(range(6), x)) # -1; 1st list ends early
+print(fcmp(x, range(6))) # 1; 2nd list ends early
+print(fcmp(range(8), x)) # 1; 2nd list ends early
+print(fcmp(x, range(8))) # -1; 1st list ends early
+print(fcmp([1,[[2],8]],
+ [[[1],2],8])) # 0
+print(fcmp([1,[[3],8]],
+ [[[1],2],8])) # 1
+print(fcmp([1,[[2],8]],
+ [[[1],2],9])) # -1
# end of example
diff --git a/Demo/threads/find.py b/Demo/threads/find.py
index 68ca155..57fe81e 100644
--- a/Demo/threads/find.py
+++ b/Demo/threads/find.py
@@ -123,7 +123,7 @@ def main():
def selector(dir, name, fullname, stat):
# Look for world writable files that are not symlinks
- return (stat[ST_MODE] & 0002) != 0 and not S_ISLNK(stat[ST_MODE])
+ return (stat[ST_MODE] & 0o002) != 0 and not S_ISLNK(stat[ST_MODE])
# The find procedure -- calls wq.addwork() for subdirectories
@@ -132,7 +132,7 @@ def find(dir, pred, wq):
try:
names = os.listdir(dir)
except os.error as msg:
- print repr(dir), ':', msg
+ print(repr(dir), ':', msg)
return
for name in names:
if name not in (os.curdir, os.pardir):
@@ -140,10 +140,10 @@ def find(dir, pred, wq):
try:
stat = os.lstat(fullname)
except os.error as msg:
- print repr(fullname), ':', msg
+ print(repr(fullname), ':', msg)
continue
if pred(dir, name, fullname, stat):
- print fullname
+ print(fullname)
if S_ISDIR(stat[ST_MODE]):
if not os.path.ismount(fullname):
wq.addwork(find, (fullname, pred, wq))
diff --git a/Demo/threads/squasher.py b/Demo/threads/squasher.py
index 0d59cb8..35b1b1d 100644
--- a/Demo/threads/squasher.py
+++ b/Demo/threads/squasher.py
@@ -89,7 +89,7 @@ def assembler():
def putline():
while 1:
line = co.tran(coassembler)
- print line
+ print(line)
import string
co = Coroutine()
@@ -100,6 +100,6 @@ codisassembler = co.create(disassembler)
cosquasher = co.create(squasher)
co.tran(coputline)
-print 'done'
+print('done')
# end of example
diff --git a/Demo/threads/sync.py b/Demo/threads/sync.py
index 843767a..61e1628 100644
--- a/Demo/threads/sync.py
+++ b/Demo/threads/sync.py
@@ -280,8 +280,8 @@ class condition:
hasattr(lock, 'release'):
self.mutex = lock
else:
- raise TypeError, 'condition constructor requires ' \
- 'a lock argument'
+ raise TypeError('condition constructor requires ' \
+ 'a lock argument')
# lock used to block threads until a signal
self.checkout = thread.allocate_lock()
@@ -304,8 +304,7 @@ class condition:
def wait(self):
mutex, checkout, idlock = self.mutex, self.checkout, self.idlock
if not mutex.locked():
- raise ValueError, \
- "condition must be .acquire'd when .wait() invoked"
+ raise ValueError("condition must be .acquire'd when .wait() invoked")
idlock.acquire()
myid = self.id
@@ -336,7 +335,7 @@ class condition:
def broadcast(self, num = -1):
if num < -1:
- raise ValueError, '.broadcast called with num %r' % (num,)
+ raise ValueError('.broadcast called with num %r' % (num,))
if num == 0:
return
self.idlock.acquire()
@@ -402,7 +401,7 @@ class event:
class semaphore:
def __init__(self, count=1):
if count <= 0:
- raise ValueError, 'semaphore count %d; must be >= 1' % count
+ raise ValueError('semaphore count %d; must be >= 1' % count)
self.count = count
self.maxcount = count
self.nonzero = condition()
@@ -417,8 +416,8 @@ class semaphore:
def v(self):
self.nonzero.acquire()
if self.count == self.maxcount:
- raise ValueError, '.v() tried to raise semaphore count above ' \
- 'initial value %r' % self.maxcount
+ raise ValueError('.v() tried to raise semaphore count above ' \
+ 'initial value %r' % self.maxcount)
self.count = self.count + 1
self.nonzero.signal()
self.nonzero.release()
@@ -445,8 +444,7 @@ class mrsw:
def read_out(self):
self.rwOK.acquire()
if self.nr <= 0:
- raise ValueError, \
- '.read_out() invoked without an active reader'
+ raise ValueError('.read_out() invoked without an active reader')
self.nr = self.nr - 1
if self.nr == 0:
self.writeOK.signal()
@@ -463,8 +461,7 @@ class mrsw:
def write_out(self):
self.rwOK.acquire()
if not self.writing:
- raise ValueError, \
- '.write_out() invoked without an active writer'
+ raise ValueError('.write_out() invoked without an active writer')
self.writing = 0
self.nw = self.nw - 1
if self.nw:
@@ -476,8 +473,7 @@ class mrsw:
def write_to_read(self):
self.rwOK.acquire()
if not self.writing:
- raise ValueError, \
- '.write_to_read() invoked without an active writer'
+ raise ValueError('.write_to_read() invoked without an active writer')
self.writing = 0
self.nw = self.nw - 1
self.nr = self.nr + 1
@@ -496,13 +492,13 @@ def _new_thread(func, *args):
global TID
tid.acquire(); id = TID = TID+1; tid.release()
io.acquire(); alive.append(id); \
- print 'starting thread', id, '--', len(alive), 'alive'; \
+ print('starting thread', id, '--', len(alive), 'alive'); \
io.release()
thread.start_new_thread( func, (id,) + args )
def _qsort(tid, a, l, r, finished):
# sort a[l:r]; post finished when done
- io.acquire(); print 'thread', tid, 'qsort', l, r; io.release()
+ io.acquire(); print('thread', tid, 'qsort', l, r); io.release()
if r-l > 1:
pivot = a[l]
j = l+1 # make a[l:j] <= pivot, and a[j:r] > pivot
@@ -519,44 +515,44 @@ def _qsort(tid, a, l, r, finished):
l_subarray_sorted.wait()
r_subarray_sorted.wait()
- io.acquire(); print 'thread', tid, 'qsort done'; \
+ io.acquire(); print('thread', tid, 'qsort done'); \
alive.remove(tid); io.release()
finished.post()
def _randarray(tid, a, finished):
- io.acquire(); print 'thread', tid, 'randomizing array'; \
+ io.acquire(); print('thread', tid, 'randomizing array'); \
io.release()
for i in range(1, len(a)):
wh.acquire(); j = randint(0,i); wh.release()
a[i], a[j] = a[j], a[i]
- io.acquire(); print 'thread', tid, 'randomizing done'; \
+ io.acquire(); print('thread', tid, 'randomizing done'); \
alive.remove(tid); io.release()
finished.post()
def _check_sort(a):
if a != range(len(a)):
- raise ValueError, ('a not sorted', a)
+ raise ValueError('a not sorted', a)
def _run_one_sort(tid, a, bar, done):
# randomize a, and quicksort it
# for variety, all the threads running this enter a barrier
# at the end, and post `done' after the barrier exits
- io.acquire(); print 'thread', tid, 'randomizing', a; \
+ io.acquire(); print('thread', tid, 'randomizing', a); \
io.release()
finished = event()
_new_thread(_randarray, a, finished)
finished.wait()
- io.acquire(); print 'thread', tid, 'sorting', a; io.release()
+ io.acquire(); print('thread', tid, 'sorting', a); io.release()
finished.clear()
_new_thread(_qsort, a, 0, len(a), finished)
finished.wait()
_check_sort(a)
- io.acquire(); print 'thread', tid, 'entering barrier'; \
+ io.acquire(); print('thread', tid, 'entering barrier'); \
io.release()
bar.enter()
- io.acquire(); print 'thread', tid, 'leaving barrier'; \
+ io.acquire(); print('thread', tid, 'leaving barrier'); \
io.release()
io.acquire(); alive.remove(tid); io.release()
bar.enter() # make sure they've all removed themselves from alive
@@ -586,16 +582,16 @@ def test():
_new_thread(_run_one_sort, arrays[i], bar, finished)
finished.wait()
- print 'all threads done, and checking results ...'
+ print('all threads done, and checking results ...')
if alive:
- raise ValueError, ('threads still alive at end', alive)
+ raise ValueError('threads still alive at end', alive)
for i in range(NSORTS):
a = arrays[i]
if len(a) != (i+1)*10:
- raise ValueError, ('length of array', i, 'screwed up')
+ raise ValueError('length of array', i, 'screwed up')
_check_sort(a)
- print 'test passed!', TID, 'threads created in all'
+ print('test passed!', TID, 'threads created in all')
if __name__ == '__main__':
test()
diff --git a/Demo/threads/telnet.py b/Demo/threads/telnet.py
index 09f3bd9..7366341 100644
--- a/Demo/threads/telnet.py
+++ b/Demo/threads/telnet.py
@@ -76,7 +76,7 @@ def parent(s):
cleandata = ''
for c in data:
if opt:
- print ord(c)
+ print(ord(c))
## print '(replying: %r)' % (opt+c,)
s.send(opt + c)
opt = ''
@@ -85,18 +85,18 @@ def parent(s):
if c == IAC:
cleandata = cleandata + c
elif c in (DO, DONT):
- if c == DO: print '(DO)',
- else: print '(DONT)',
+ if c == DO: print('(DO)', end=' ')
+ else: print('(DONT)', end=' ')
opt = IAC + WONT
elif c in (WILL, WONT):
- if c == WILL: print '(WILL)',
- else: print '(WONT)',
+ if c == WILL: print('(WILL)', end=' ')
+ else: print('(WONT)', end=' ')
opt = IAC + DONT
else:
- print '(command)', ord(c)
+ print('(command)', ord(c))
elif c == IAC:
iac = 1
- print '(IAC)',
+ print('(IAC)', end=' ')
else:
cleandata = cleandata + c
sys.stdout.write(cleandata)