global scheduled, scheduler, wrapper_ref scheduled = [] from ctypes import pythonapi, cast, c_char_p, c_void_p, CFUNCTYPE HOOKFUNC = CFUNCTYPE(c_char_p, c_void_p, c_void_p, c_char_p) PyOS_InputHookFunctionPointer = c_void_p.in_dll(pythonapi, "PyOS_InputHook") def scheduler(*a, **k): # *no effect : pthread_sigmask( SIG_BLOCK, [SIGWINCH,SIGINT]) # *no effect : oldmask = pthread_sigmask(signal.SIG_BLOCK, [SIGWINCH,SIGINT]) global scheduled # prevent reenter lq = len(scheduled) while lq: fn, a = scheduled.pop(0) fn(a) lq -= 1 # *no effect : pthread_sigmask( SIG_UNBLOCK, [SIGWINCH,SIGINT] ) # *no effect : pthread_sigmask( SIG_UNBLOCK, oldmask ) wrapper_ref = HOOKFUNC(scheduler) PyOS_InputHookFunctionPointer.value = cast(wrapper_ref, c_void_p).value def schedule(fn, a): global scheduled if scheduled is None: init() scheduled.append((fn, a)) import os,time os.system(f'( sleep 1 && for x in {{a..z}}; do kill -28 {os.getpid()}; done) &') def loop_hook(arg): time.sleep(.016) schedule(loop_hook,1) schedule(loop_hook,1)