def test_timing_02(capsys): '''stopping in state STARTED after action''' ts = Timespan() t = Task(print_it, args=(ts, 'started')) + Task( print_it, args=(ts, 'hello'), action_stop=print_it, args_stop=(ts, 'stopped'), action_cont=print_it, args_cont=(ts, 'continued'), duration=.2) + Task(print_it, args=(ts, 'finished')) t.start(.1) sleep(.2) assert t.state == STATE_STARTED assert t.activity == ACTIVITY_SLEEP t.stop().join() assert t.state == STATE_STOPPED assert t.activity == ACTIVITY_NONE captured = capsys.readouterr() assert captured.err == '' assert captured.out == '0.1:started 0.1:hello 0.2:stopped ' sleep(.1) t.cont().join() assert t.state == STATE_FINISHED assert t.activity == ACTIVITY_NONE captured = capsys.readouterr() assert captured.err == '' assert captured.out == '0.3:continued 0.4:finished '
def test_standard(capsys): ts = Timespan() acc = Accelerate(ts, 0.3) t = Task(print_it, args=(ts, 'started')) + Repeated( acc.step, action_stop=print_it, args_stop=(ts, 'stopped'), action_cont=print_it, args_cont=(ts, 'continued')) + Task(print_it, args=(ts, 'finished')) t.start() sleep(.4) assert t.state == STATE_STARTED assert t.activity == ACTIVITY_SLEEP t.stop().join() assert t.state == STATE_STOPPED assert t.activity == ACTIVITY_NONE captured = capsys.readouterr() assert captured.err == '' assert captured.out == \ '0.0:started 0.0:hi 0.3:hi 0.4:stopped ' t.cont(.1).join() assert t.state == STATE_FINISHED assert t.activity == ACTIVITY_NONE captured = capsys.readouterr() assert captured.out == \ '0.5:continued 0.6:hi 0.7:hi 0.7:hi ' + \ '0.7:finished '
def test_action_start(capsys): # without delay t = Task( print, args=('started', ), kwargs={'end': ' '}, ) + Task(print, args=('hello, world!', ), kwargs={'end': ''}, action_stop=print, args_stop=(' stopped', ), kwargs_stop={'end': ''}) t.start() t.join() assert t.state == STATE_FINISHED captured = capsys.readouterr() assert captured.err == '' assert captured.out == 'started hello, world!' # with delay t.start(.1) sleep(.05) assert t.state == STATE_TO_START t.stop().join() assert t.state == STATE_STOPPED captured = capsys.readouterr() assert captured.err == '' assert captured.out == '' t.cont().join() captured = capsys.readouterr() assert t.state == STATE_FINISHED assert captured.err == '' assert captured.out == 'started hello, world!'
def test_action_final(capsys): t = Task(print, args=('hello, world!', ), kwargs={'end': ''}) + Task( print, args=(' finished', ), kwargs={'end': ''}) t.start() t.join() captured = capsys.readouterr() assert t.state == STATE_FINISHED assert captured.err == '' assert captured.out == 'hello, world! finished'
def test_repeated(capsys): ts = Timespan() t = Task( print_it, args=(ts, 'parent_started') ) + Repeated( Task( print_it, args=(ts, 'child_started'), ) + Task( print_it, duration=.2, args=(ts, 'child'), action_stop=print_it, args_stop=(ts, 'child_stopped'), action_cont=print_it, args_cont=(ts, 'child_continued') ) + Task( print_it, args=(ts, 'child_finished') ), num=2, action_stop=print_it, args_stop=(ts, 'parent_stopped'), action_cont=print_it, args_cont=(ts, 'parent_continued') ) + Task( print_it, args=(ts, 'parent_finished') ) t.start() sleep(.3) assert t.state == STATE_STARTED assert t.activity == ACTIVITY_BUSY t.stop().join() assert t.state == STATE_STOPPED assert t.activity == ACTIVITY_NONE captured = capsys.readouterr() assert captured.err == '' assert captured.out == \ '0.0:parent_started 0.0:child_started 0.0:child ' + \ '0.2:child_finished 0.2:child_started 0.2:child ' + \ '0.3:child_stopped ' sleep(.1) t.cont(thread=False) assert t.state == STATE_FINISHED captured = capsys.readouterr() assert captured.err == '' assert captured.out == \ '0.4:child_continued ' + \ '0.5:child_finished 0.5:parent_finished '
def test_standard(capsys): ts = Timespan() t = Task(print_it, args=(ts, 'started')) + Sleep(.1) + Task( print_it, args=(ts, 'finished')) t.start().join() captured = capsys.readouterr() assert t.state == STATE_FINISHED assert captured.err == '' assert captured.out == '0.0:started 0.1:finished '
def test_timing_00(capsys): ts = Timespan() t = Task(print_it, args=(ts, 'started')) + Task( print_it, args=(ts, 'hello'), duration=.1) + Task( print_it, args=(ts, 'finished')) t.start(.1).join() captured = capsys.readouterr() assert t.state == STATE_FINISHED assert captured.err == '' assert captured.out == '0.1:started 0.1:hello 0.2:finished '
def test_long_lasting(capsys): ts = Timespan() t = Task(print_it, args=(ts, 'started')) + Repeated( print_it_long_lasting, args=(ts, 'hi'), num=2, duration=.3) + Task( print_it, args=(ts, 'finished')) t.start().join() captured = capsys.readouterr() assert t.state == STATE_FINISHED assert captured.err == '' assert captured.out == '0.0:started 0.0:hi 0.1:hi 0.3:finished '
def test_threadless_child(capsys): ts = Timespan() t = Task(print_it, args=(ts, 'started')) + Periodic( .1, Task(print_it, args=(ts, 'hi')), num=3) + Task( print_it, args=(ts, 'finished')) t.start(thread=False) captured = capsys.readouterr() assert t.state == STATE_FINISHED assert captured.err == '' assert captured.out == \ '0.0:started 0.0:hi 0.1:hi 0.2:hi 0.2:finished '
def test_netto_time(capsys): ts = Timespan() t = Task(print_it, args=(ts, 'started')) + Periodic( .1, print_it_long_lasting, args=(ts, 'hi'), num=3, netto_time=True) + Task(print_it, args=(ts, 'finished')) t.start().join() captured = capsys.readouterr() assert t.state == STATE_FINISHED assert captured.err == '' assert captured.out == \ '0.0:started 0.0:hi 0.2:hi 0.4:hi 0.5:finished '
def test_standard(capsys): ts = Timespan() t = Task(print_it, args=(ts, 'started')) + Periodic( .1, print_it, args=(ts, 'hi'), num=3, ) + Task(print_it, args=(ts, 'finished')) t.start().join() captured = capsys.readouterr() assert t.state == STATE_FINISHED assert captured.err == '' assert captured.out == \ '0.0:started 0.0:hi 0.1:hi 0.2:hi 0.2:finished '
def test_action_stop(capsys): t = Task(print, args=('hello, world!', ), kwargs={'end': ''}, duration=.1, action_stop=print, args_stop=(' stopped', ), kwargs_stop={'end': ''}) + Task( print, args=(' finished', ), kwargs={'end': ''}) t.start() sleep(.05) t.stop() t.join() captured = capsys.readouterr() assert t.state == STATE_STOPPED assert captured.err == '' assert captured.out == 'hello, world! stopped'
def test_action_cont(capsys): # with duration t = Task(print, args=('hello, world!', ), kwargs={'end': ''}, duration=.1, action_stop=print, args_stop=(' stopped', ), kwargs_stop={'end': ''}, action_cont=print, args_cont=(' continued', ), kwargs_cont={'end': ''}) + Task( print, args=(' finished', ), kwargs={'end': ''}) t.start().stop() t.join() assert t.state == STATE_STOPPED t.cont().join() captured = capsys.readouterr() assert t.state == STATE_FINISHED assert captured.err == '' assert captured.out == 'hello, world! stopped continued finished' # without duration t = Task(print, args=('hello, world!', ), kwargs={'end': ''}, action_stop=print, args_stop=(' stopped', ), kwargs_stop={'end': ''}, action_cont=print, args_cont=(' continued', ), kwargs_cont={'end': ''}) + Task( print, args=(' finished', ), kwargs={'end': ''}) t.start().stop() t.join() assert t.state == STATE_FINISHED t.cont().join() captured = capsys.readouterr() assert t.state == STATE_FINISHED assert captured.err == '' assert captured.out == 'hello, world! finished'
def test_timing_01(capsys): '''stopping in state TO_START (in delay, before started)''' ts = Timespan() t = Task(print_it, args=(ts, 'started')) + Task( print_it, args=(ts, 'hello'), duration=.1) + Task( print_it, args=(ts, 'finished')) t.start(.2) sleep(.1) assert t.state == STATE_TO_START t.stop().join() assert t.state == STATE_STOPPED sleep(.1) t.cont().join() assert t.state == STATE_FINISHED captured = capsys.readouterr() assert captured.err == '' assert captured.out == '0.3:started 0.3:hello 0.4:finished '
def test_join_01(capsys): '''joining a task, that is no child''' ts = Timespan() t1 = Task( print_it, args=(ts, 't1_started'), ) + Task( print_it, duration=.3, args=(ts, 't1'), action_stop=print_it, args_stop=(ts, 't1_stopped'), action_cont=print_it, args_cont=(ts, 't1_continued') ) + Task( print_it, args=(ts, 't1_finished') ) t2 = concat( Task( print_it, args=(ts, 't2_started') ), Task( print_it, args=(ts, 't2') ), Task( t1.join, action_stop=print_it, args_stop=(ts, 't2_stopped'), action_cont=print_it, args_cont=(ts, 't2_continued') ), Task( print_it, args=(ts, 't2_finished') ) ) t1.start() t2.start(.1) sleep(.001) assert t1.state == STATE_STARTED assert t1.activity == ACTIVITY_SLEEP assert t2.state == STATE_TO_START assert t2.activity == ACTIVITY_SLEEP captured = capsys.readouterr() assert captured.err == '' assert captured.out == '0.0:t1_started 0.0:t1 ' t2.stop().join() assert t1.state == STATE_STARTED assert t2.state == STATE_STOPPED t2.cont() sleep(.1) assert t1.state == STATE_STARTED assert t2.state == STATE_STARTED assert t2.activity == ACTIVITY_JOIN captured = capsys.readouterr() assert captured.err == '' assert captured.out == '0.1:t2_started 0.1:t2 ' t2.stop() sleep(.01) assert t1.state == STATE_STARTED assert t2.state == STATE_TO_STOP t2.cont().join() assert t1.state == STATE_FINISHED assert t2.state == STATE_FINISHED captured = capsys.readouterr() assert captured.err == '' assert captured.out == '0.3:t1_finished 0.3:t2_finished '
def test_periodic(capsys): ts = Timespan() t = Task( print_it, args=(ts, 'parent_started') ) + Periodic( .1, ( Task( print_it, args=(ts, 'child_started') ) + Task( print_it, args=(ts, 'child'), action_stop=print_it, args_stop=(ts, 'child_stopped'), action_cont=print_it, args_cont=(ts, 'child_continued') ) + Task( print_it, args=(ts, 'child_finished') ) ).start, args=(.2,), kwargs={'thread': False}, num=3, action_stop=print_it, args_stop=(ts, 'parent_stopped'), action_cont=print_it, args_cont=(ts, 'parent_continued') ) + Task( print_it, args=(ts, 'parent_finished') ) t.start() sleep(.25) assert t.state == STATE_STARTED assert t.activity == ACTIVITY_BUSY captured = capsys.readouterr() assert captured.err == '' assert captured.out == \ '0.0:parent_started 0.2:child_started 0.2:child ' + \ '0.2:child_finished ' sleep(.05) t.stop().join() assert t.state == STATE_STOPPED assert t.activity == ACTIVITY_NONE captured = capsys.readouterr() assert captured.err == '' assert captured.out == \ '0.3:parent_stopped ' sleep(.1) t.cont(thread=False) assert t.state == STATE_FINISHED captured = capsys.readouterr() assert captured.err == '' assert captured.out == \ '0.4:parent_continued ' + \ '0.5:child_started 0.5:child 0.5:child_finished ' + \ '0.7:child_started 0.7:child 0.7:child_finished ' + \ '0.7:parent_finished '
] for t in thread_pool: t.start() sleep(2) while not batch == POP_SIZE: print( 'This is iteration', iteration, ', {}/{}({:.2f}%) completed'.format( batch, POP_SIZE, 100 * batch / POP_SIZE)) for t in thread_pool: if not t.isAlive(): if solutions: temp_pool.append(solutions.pop()) new_thread = Task(t.addr, temp_pool[-1]) new_thread.start() thread_pool.append(new_thread) elif temp_pool: try: used = [ k.param_list for k in thread_pool if k.isAlive() ] except ValueError as e: print('get value error in used', len(thread_pool), len(temp_pool)) break # ERROR: `in` for ndarray alter `==` function candidate = [k for k in temp_pool if k not in used] if candidate: