1+ #!/usr/bin/env python2 
2+ # coding: utf-8 
3+ 
14import  logging 
25import  random 
36import  threading 
47import  time 
58import  unittest 
69
10+ import  sys 
11+ 
12+ sys .path .insert (0 ,'/Users/连长/Code/Python' )
13+ 
714from  pykit  import  jobq 
815from  pykit  import  threadutil 
916from  pykit  import  ututil 
@@ -38,7 +45,7 @@ def sleep_5(args):
3845
3946class  TestProbe (unittest .TestCase ):
4047
41-     def  _start_jobq_in_thread (self , n_items , n_worker , keep_order = False ):
48+     def  _start_jobq_in_thread (self , items , n_worker , keep_order = False ,  partial_order = False ):
4249
4350        def  _sleep_1 (args ):
4451            time .sleep (0.1 )
@@ -48,11 +55,12 @@ def _nothing(args):
4855            return  args 
4956
5057        probe  =  {}
51-         th  =  threading .Thread (target = lambda : jobq .run (range ( n_items ) ,
58+         th  =  threading .Thread (target = lambda : jobq .run (items ,
5259                                                      [(_sleep_1 , n_worker ),
5360                                                       _nothing ],
5461                                                      probe = probe ,
5562                                                      keep_order = keep_order ,
63+                                                       partial_order = partial_order ,
5664                                                      ))
5765        th .daemon  =  True 
5866        th .start ()
@@ -67,13 +75,13 @@ def test_probe_single_thread(self):
6775                (0.2 ,  0 , 'all done' ),
6876        )
6977
70-         th , probe  =  self ._start_jobq_in_thread (3 , 1 )
78+         th , probe  =  self ._start_jobq_in_thread (range ( 3 ) , 1 )
7179
7280        for  sleep_time , doing , case_mes  in  cases :
7381
7482            time .sleep (sleep_time )
7583            stat  =  jobq .stat (probe )
76- 
84+              print ( stat ) 
7785            self .assertEqual (doing , stat ['doing' ], case_mes )
7886
7987            # qsize() is not reliable. do not test the value of it. 
@@ -100,7 +108,7 @@ def test_probe_3_thread(self):
100108                (0.4 ,  0 , 'all done' ),
101109        )
102110
103-         th , probe  =  self ._start_jobq_in_thread (10 , 3 )
111+         th , probe  =  self ._start_jobq_in_thread (range ( 10 ) , 3 )
104112
105113        for  sleep_time , doing , case_mes  in  cases :
106114
@@ -124,7 +132,7 @@ def test_probe_3_thread_keep_order(self):
124132                (0.4 ,  0 , 'all done' ),
125133        )
126134
127-         th , probe  =  self ._start_jobq_in_thread (10 , 3 , keep_order = True )
135+         th , probe  =  self ._start_jobq_in_thread (range ( 10 ) , 3 , keep_order = True )
128136
129137        for  sleep_time , doing , case_mes  in  cases :
130138
@@ -140,6 +148,28 @@ def test_probe_3_thread_keep_order(self):
140148
141149        th .join ()
142150
151+     def  test_probe_3_thread_partial_order (self ):
152+         cases  =  (
153+             (0.05 , 3 , '_sleep_1 is working on 1st 3 items' ),
154+             (0.1 , 3 , '_sleep_1 is working on 2nd 3 items' ),
155+             (0.4 , 0 , 'all done' ),
156+         )
157+ 
158+         th , probe  =  self ._start_jobq_in_thread (([i , 0 ] for  i  in  range (10 )), 3 , partial_order = True )
159+ 
160+         for  sleep_time , doing , case_mes  in  cases :
161+             time .sleep (sleep_time )
162+             stat  =  jobq .stat (probe )
163+ 
164+             self .assertEqual (doing , stat ['doing' ], case_mes )
165+ 
166+         # use the last stat 
167+ 
168+         workers  =  stat ['workers' ]
169+         self .assertEqual (2 , len (workers ))
170+ 
171+         th .join ()
172+ 
143173
144174class  TestDispatcher (unittest .TestCase ):
145175
@@ -434,6 +464,47 @@ def _change_thread_nr():
434464        for  th  in  ths :
435465            th .join ()
436466
467+     def  test_set_thread_num_partial_order (self ):
468+ 
469+         def  _pass (args ):
470+             print (args )
471+             return  args 
472+ 
473+         rst  =  []
474+ 
475+         jm  =  jobq .JobManager ([_pass , (rst .append ,5 )], partial_order = True )
476+         # jm = jobq.JobManager([_pass, rst.append]) 
477+ 
478+         # setter = {'running': True} 
479+ 
480+         # def _change_thread_nr(): 
481+         #     while setter['running']: 
482+         #         jm.set_thread_num(_pass, random.randint(1, 4)) 
483+         #         time.sleep(0.5) 
484+         # 
485+         # ths = [] 
486+         # for ii in range(3): 
487+         #     th = threadutil.start_daemon_thread(_change_thread_nr) 
488+         #     ths.append(th) 
489+ 
490+         n  =  60 
491+         for  i  in  range (n ):
492+             jm .put ([i  %  2 , i ])
493+ 
494+         jm .join ()
495+ 
496+         # rst.sort() 
497+         print ('' )
498+         print (rst )
499+         for  i  in  range (n ):
500+             self .assertEqual (i  %  2 , rst [i ][0 ])
501+             self .assertEqual (i , rst [i ][1 ])
502+         # 
503+         # setter['running'] = False 
504+         # 
505+         # for th in ths: 
506+         #     th.join() 
507+ 
437508
438509class  TestJobQ (unittest .TestCase ):
439510
0 commit comments