Skip to content

Updates for demo 23 #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Demos/Demo23/Unit1.dfm
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object Form1: TForm1
Font.Name = 'Consolas'
Font.Style = []
Lines.Strings = (
'import threading'
'import threading_test'
'import sys'
'try:'
' count = int(sys.argv[1])'
Expand All @@ -35,7 +35,7 @@ object Form1: TForm1
''
'for i in range(count):'
' print ("**** Pass", i)'
' threading._test()'
' threading_test._test()'
'print ("**** Done.")')
ParentFont = False
ScrollBars = ssBoth
Expand Down
96 changes: 96 additions & 0 deletions Demos/Demo23/threading_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
from threading import *
from collections import deque
from time import sleep

def _test():

class BoundedQueue():

def __init__(self, limit):
self.mon = RLock()
self.rc = Condition(self.mon)
self.wc = Condition(self.mon)
self.limit = limit
self.queue = deque()

def put(self, item):
self.mon.acquire()
while len(self.queue) >= self.limit:
self._note("put(%s): queue full", item)
self.wc.wait()
self.queue.append(item)
self._note("put(%s): appended, length now %d",
item, len(self.queue))
self.rc.notify()
self.mon.release()

def get(self):
self.mon.acquire()
while not self.queue:
self._note("get(): queue empty")
self.rc.wait()
item = self.queue.popleft()
self._note("get(): got %s, %d left", item, len(self.queue))
self.wc.notify()
self.mon.release()
return item

def _note(self, format, *args):
format = format % args
ident = get_ident()
try:
name = current_thread().name
except KeyError:
name = "<OS thread %d>" % ident
format = "%s: %s" % (name, format)
print(format)

class ProducerThread(Thread):

def __init__(self, queue, quota):
Thread.__init__(self, name="Producer")
self.queue = queue
self.quota = quota

def run(self):
from random import random
counter = 0
while counter < self.quota:
counter = counter + 1
self.queue.put("%s.%d" % (self.name, counter))
sleep(random() * 0.00001)


class ConsumerThread(Thread):

def __init__(self, queue, count):
Thread.__init__(self, name="Consumer")
self.queue = queue
self.count = count

def run(self):
while self.count > 0:
item = self.queue.get()
print(item)
self.count = self.count - 1

NP = 3
QL = 4
NI = 5

Q = BoundedQueue(QL)
P = []
for i in range(NP):
t = ProducerThread(Q, NI)
t.name = ("Producer-%d" % (i+1))
P.append(t)
C = ConsumerThread(Q, NI*NP)
for t in P:
t.start()
sleep(0.000001)
C.start()
for t in P:
t.join()
C.join()

_test()