forked from AllenDowney/ThinkPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.py
More file actions
22 lines (15 loc) · 439 Bytes
/
Copy paththread.py
File metadata and controls
22 lines (15 loc) · 439 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
"""Example code using Python threads.
Copyright 2010 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""
from threading import Thread
from time import sleep
def counter(xs, delay=1):
for x in xs:
print x
sleep(delay)
# one thread counts backwards, fast
t = Thread(target=counter, args=[range(100, 1, -1), 0.25])
t.start()
# the other thread count forwards, slow
counter(range(1, 100), 1)