Back to index

python3.2  3.2.2
Public Member Functions
test.test_capi.TestPendingCalls Class Reference

List of all members.

Public Member Functions

def pendingcalls_submit
def pendingcalls_wait
def test_pendingcalls_threaded
def pendingcalls_thread
def test_pendingcalls_non_threaded

Detailed Description

Definition at line 58 of file test_capi.py.


Member Function Documentation

Definition at line 60 of file test_capi.py.

00060 
00061     def pendingcalls_submit(self, l, n):
00062         def callback():
00063             #this function can be interrupted by thread switching so let's
00064             #use an atomic operation
00065             l.append(None)
00066 
00067         for i in range(n):
00068             time.sleep(random.random()*0.02) #0.01 secs on average
00069             #try submitting callback until successful.
00070             #rely on regular interrupt to flush queue if we are
00071             #unsuccessful.
00072             while True:
00073                 if _testcapi._pending_threadfunc(callback):
00074                     break;

Here is the caller graph for this function:

Definition at line 118 of file test_capi.py.

00118 
00119     def pendingcalls_thread(self, context):
00120         try:
00121             self.pendingcalls_submit(context.l, context.n)
00122         finally:
00123             with context.lock:
00124                 context.nFinished += 1
00125                 nFinished = context.nFinished
00126                 if False and support.verbose:
00127                     print("finished threads: ", nFinished)
00128             if nFinished == context.nThreads:
00129                 context.event.set()

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_capi.TestPendingCalls.pendingcalls_wait (   self,
  l,
  n,
  context = None 
)

Definition at line 75 of file test_capi.py.

00075 
00076     def pendingcalls_wait(self, l, n, context = None):
00077         #now, stick around until l[0] has grown to 10
00078         count = 0;
00079         while len(l) != n:
00080             #this busy loop is where we expect to be interrupted to
00081             #run our callbacks.  Note that callbacks are only run on the
00082             #main thread
00083             if False and support.verbose:
00084                 print("(%i)"%(len(l),),)
00085             for i in range(1000):
00086                 a = i*i
00087             if context and not context.event.is_set():
00088                 continue
00089             count += 1
00090             self.assertTrue(count < 10000,
00091                 "timeout waiting for %i callbacks, got %i"%(n, len(l)))
00092         if False and support.verbose:
00093             print("(%i)"%(len(l),))

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 130 of file test_capi.py.

00130 
00131     def test_pendingcalls_non_threaded(self):
00132         #again, just using the main thread, likely they will all be dispatched at
00133         #once.  It is ok to ask for too many, because we loop until we find a slot.
00134         #the loop can be interrupted to dispatch.
00135         #there are only 32 dispatch slots, so we go for twice that!
00136         l = []
00137         n = 64
00138         self.pendingcalls_submit(l, n)
00139         self.pendingcalls_wait(l, n)
00140 
# Bug #6012

Here is the call graph for this function:

Definition at line 94 of file test_capi.py.

00094 
00095     def test_pendingcalls_threaded(self):
00096 
00097         #do every callback on a separate thread
00098         n = 32 #total callbacks
00099         threads = []
00100         class foo(object):pass
00101         context = foo()
00102         context.l = []
00103         context.n = 2 #submits per thread
00104         context.nThreads = n // context.n
00105         context.nFinished = 0
00106         context.lock = threading.Lock()
00107         context.event = threading.Event()
00108 
00109         for i in range(context.nThreads):
00110             t = threading.Thread(target=self.pendingcalls_thread, args = (context,))
00111             t.start()
00112             threads.append(t)
00113 
00114         self.pendingcalls_wait(context.l, n, context)
00115 
00116         for t in threads:
00117             t.join()

Here is the call graph for this function:


The documentation for this class was generated from the following file: