Skip to content

Commit 159b7e4

Browse files
committed
NumberServices unit test added
1 parent 1811c1a commit 159b7e4

File tree

6 files changed

+920
-38
lines changed

6 files changed

+920
-38
lines changed

Demos/Demo22/threading_test.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
from threading import *
2+
from collections import deque
3+
from time import sleep
4+
5+
def _test():
6+
7+
class BoundedQueue():
8+
9+
def __init__(self, limit):
10+
self.mon = RLock()
11+
self.rc = Condition(self.mon)
12+
self.wc = Condition(self.mon)
13+
self.limit = limit
14+
self.queue = deque()
15+
16+
def put(self, item):
17+
self.mon.acquire()
18+
while len(self.queue) >= self.limit:
19+
self._note("put(%s): queue full", item)
20+
self.wc.wait()
21+
self.queue.append(item)
22+
self._note("put(%s): appended, length now %d",
23+
item, len(self.queue))
24+
self.rc.notify()
25+
self.mon.release()
26+
27+
def get(self):
28+
self.mon.acquire()
29+
while not self.queue:
30+
self._note("get(): queue empty")
31+
self.rc.wait()
32+
item = self.queue.popleft()
33+
self._note("get(): got %s, %d left", item, len(self.queue))
34+
self.wc.notify()
35+
self.mon.release()
36+
return item
37+
38+
def _note(self, format, *args):
39+
format = format % args
40+
ident = get_ident()
41+
try:
42+
name = current_thread().name
43+
except KeyError:
44+
name = "<OS thread %d>" % ident
45+
format = "%s: %s" % (name, format)
46+
print(format)
47+
48+
class ProducerThread(Thread):
49+
50+
def __init__(self, queue, quota):
51+
Thread.__init__(self, name="Producer")
52+
self.queue = queue
53+
self.quota = quota
54+
55+
def run(self):
56+
from random import random
57+
counter = 0
58+
while counter < self.quota:
59+
counter = counter + 1
60+
self.queue.put("%s.%d" % (self.name, counter))
61+
sleep(random() * 0.00001)
62+
63+
64+
class ConsumerThread(Thread):
65+
66+
def __init__(self, queue, count):
67+
Thread.__init__(self, name="Consumer")
68+
self.queue = queue
69+
self.count = count
70+
71+
def run(self):
72+
while self.count > 0:
73+
item = self.queue.get()
74+
print(item)
75+
self.count = self.count - 1
76+
77+
NP = 3
78+
QL = 4
79+
NI = 5
80+
81+
Q = BoundedQueue(QL)
82+
P = []
83+
for i in range(NP):
84+
t = ProducerThread(Q, NI)
85+
t.name = ("Producer-%d" % (i+1))
86+
P.append(t)
87+
C = ConsumerThread(Q, NI*NP)
88+
for t in P:
89+
t.start()
90+
sleep(0.000001)
91+
C.start()
92+
for t in P:
93+
t.join()
94+
C.join()
95+
96+
_test()

Source/PythonEngine.pas

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4345,7 +4345,7 @@ function TPythonEngine.IsType(ob: PPyObject; obt: PPyTypeObject): Boolean;
43454345
result := ob^.ob_type = obt;
43464346
end;
43474347

4348-
function TPythonEngine.EvalPyFunction(pyfunc, pyargs:PPyObject): Variant;
4348+
function TPythonEngine.EvalPyFunction(pyfunc, pyargs:PPyObject): Variant;
43494349
var presult :PPyObject;
43504350
begin
43514351
CheckPython;
@@ -4378,7 +4378,7 @@ function TPythonEngine.EvalPyFunction(pyfunc, pyargs:PPyObject): Variant;
43784378
end;
43794379
end;
43804380

4381-
function TPythonEngine.EvalFunction(pyfunc:PPyObject; args: array of const): Variant;
4381+
function TPythonEngine.EvalFunction(pyfunc:PPyObject; args: array of const): Variant;
43824382
var pargs: PPyObject;
43834383
begin
43844384
CheckPython;
@@ -4390,7 +4390,7 @@ function TPythonEngine.EvalFunction(pyfunc:PPyObject; args: array of const): V
43904390
end;
43914391
end;
43924392

4393-
function TPythonEngine.EvalFunctionNoArgs(pyfunc:PPyObject): Variant;
4393+
function TPythonEngine.EvalFunctionNoArgs(pyfunc:PPyObject): Variant;
43944394
var pargs: PPyObject;
43954395
begin
43964396
CheckPython;
@@ -4402,12 +4402,12 @@ function TPythonEngine.EvalFunctionNoArgs(pyfunc:PPyObject): Variant;
44024402
end;
44034403
end;
44044404

4405-
function TPythonEngine.EvalStringAsStr(const command : AnsiString) : string;
4405+
function TPythonEngine.EvalStringAsStr(const command : AnsiString) : string;
44064406
begin
44074407
Result := Run_CommandAsString( command, eval_input );
44084408
end;
44094409

4410-
function TPythonEngine.EvalString(const command : AnsiString) : PPyObject;
4410+
function TPythonEngine.EvalString(const command : AnsiString) : PPyObject;
44114411
begin
44124412
Result := Run_CommandAsObject( command, eval_input );
44134413
end;
@@ -4417,7 +4417,7 @@ procedure TPythonEngine.ExecString(const command : AnsiString);
44174417
Py_XDecRef( Run_CommandAsObject( command, file_input ) );
44184418
end;
44194419

4420-
function TPythonEngine.Run_CommandAsString(const command : AnsiString; mode : Integer) : string;
4420+
function TPythonEngine.Run_CommandAsString(const command : AnsiString; mode : Integer) : string;
44214421
var
44224422
v : PPyObject;
44234423
begin
@@ -4427,7 +4427,7 @@ function TPythonEngine.Run_CommandAsString(const command : AnsiString; mode :
44274427
Py_XDECREF(v);
44284428
end;
44294429

4430-
function TPythonEngine.Run_CommandAsObject(const command : AnsiString; mode : Integer) : PPyObject;
4430+
function TPythonEngine.Run_CommandAsObject(const command : AnsiString; mode : Integer) : PPyObject;
44314431
begin
44324432
Result := Run_CommandAsObjectWithDict(command, mode, nil, nil);
44334433
end;

0 commit comments

Comments
 (0)