Kaydet (Commit) 7e7912f2 authored tarafından Guido van Rossum's avatar Guido van Rossum

Protect all uses of the random generator with a lock.

Particles break out of their loop when the main loop exits.
üst 3d3a52aa
# An example of a multi-threaded Tkinter program. # Brownian motion -- an example of a multi-threaded Tkinter program.
from Tkinter import * from Tkinter import *
import random import random
...@@ -14,22 +14,35 @@ RADIUS = 2 ...@@ -14,22 +14,35 @@ RADIUS = 2
LAMBDA = 10 LAMBDA = 10
FILL = 'red' FILL = 'red'
stop = 0 # Set when main loop exits
lock = threading.Lock() # Protects the random generator
def particle(canvas): def particle(canvas):
r = RADIUS r = RADIUS
x = random.gauss(WIDTH/2.0, SIGMA) lock.acquire()
y = random.gauss(HEIGHT/2.0, SIGMA) try:
x = random.gauss(WIDTH/2.0, SIGMA)
y = random.gauss(HEIGHT/2.0, SIGMA)
finally:
lock.release()
p = canvas.create_oval(x-r, y-r, x+r, y+r, fill=FILL) p = canvas.create_oval(x-r, y-r, x+r, y+r, fill=FILL)
while 1: while not stop:
dx = random.gauss(0, BUZZ) lock.acquire()
dy = random.gauss(0, BUZZ) try:
dx = random.gauss(0, BUZZ)
dy = random.gauss(0, BUZZ)
dt = random.expovariate(LAMBDA)
finally:
lock.release()
try: try:
canvas.move(p, dx, dy) canvas.move(p, dx, dy)
except TclError: except TclError:
break break
dt = random.expovariate(LAMBDA)
time.sleep(dt) time.sleep(dt)
def main(): def main():
global stop
root = Tk() root = Tk()
canvas = Canvas(root, width=WIDTH, height=HEIGHT) canvas = Canvas(root, width=WIDTH, height=HEIGHT)
canvas.pack(fill='both', expand=1) canvas.pack(fill='both', expand=1)
...@@ -39,6 +52,9 @@ def main(): ...@@ -39,6 +52,9 @@ def main():
for i in range(np): for i in range(np):
t = threading.Thread(target=particle, args=(canvas,)) t = threading.Thread(target=particle, args=(canvas,))
t.start() t.start()
root.mainloop() try:
root.mainloop()
finally:
stop = 1
main() main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment