Skip to content

Commit

Permalink
Python: threading example
Browse files Browse the repository at this point in the history
  • Loading branch information
ChandanChainani committed Mar 16, 2023
1 parent 2a48b51 commit a43db6a
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions python/threading_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import time
import random
import threading

class Hello(threading.Thread):
def __init__(self, min, max):
self.min, self.max = min, max
threading.Thread.__init__(self)

def run(self):
time.sleep(self.max)

for i in range(1000):
print(random.choice(range(self.min, self.max)))

# This creates the thread objects, but they don't do anything yet
h = Hello(3,5)
print(dir(h))
import sys; sys.exit(0)
k = Hello(0,3)

# This causes each thread to do its work
print("h start")
h.start()
print("h finished")
print("k start")
k.start()
print("k finished")

0 comments on commit a43db6a

Please sign in to comment.