0

I have a strange issue with TKinter after() method. I'm calling function func_a() (blocking call that takes some ms) in main thread and func_b() in after() to read a value at regular interval. It works like a charm, I can get some updated value during func_a() execution

I do not need any graphical interface, so I do not use anymore TKinter, now I'm calling func_a() in main thread. I create a separate thread to call func_b(). The issue is that the call to func_a() stops the execution of func_b() separate thread. I need to wait for func_a() returns to have some periodic call of func_b(). I do not have source of func_a() and func_b() (python C bindings). But maybe some thread locking mechanism prevents func_b() call when func_a() is called.

The question is, what is implemententation behind tkinter after? How can I achieve same behavior as Tkinter after(): be able to call func_b() when func_a()is called, without using TKinter?

Code looks like that :

pos_th= threading.Thread(target=read_pos, args=(0.1,))
pos_th.daemon = True
pos_th_stop = False
pos_th.start()
func_a()

def read_pos(period):
    while not pos_th_stop :
        func_b()
        time.sleep(period)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
rem
  • 1,131
  • 10
  • 15
  • ***"call to `func_a()` stops the execution of 'func_b()'"***: Remove `pos_th.daemon = True` and add `time.sleep(0.01)` after `.start()`. – stovfl Oct 22 '19 at 09:57
  • Unfortunately it does not change anything. I can for example call time.sleep(1) before func_a() call : func_b() will be called at periodic interval, but when func_as is called, it's not the case anymore – rem Oct 22 '19 at 10:14
  • ***"implemententation tkinter after"***: It's outside Python, it seems `func_a()` hold the `GIL`. Read [what-is-the-global-interpreter-lock-gil-in-cpython](https://stackoverflow.com/questions/1294382/what-is-the-global-interpreter-lock-gil-in-cpython) – stovfl Oct 22 '19 at 10:19
  • https://github.com/python/cpython/blob/master/Lib/tkinter/__init__.py#L792 – rem Oct 22 '19 at 10:28

1 Answers1

0

The question is, what is implemententation behind tkinter after?

When it comes down to it, it's really quite simple. Tkinter's mainloop method is little more than an infinite loop that waits for items to appear on the event queue. When it finds an event, it pulls it off of the queue and calls the handlers for that event.

after simply puts an item on the event queue. During each iteration of mainloop tkinter will examine the timestamp on the function added by after, and if the given amount of time has elapses, the function is pulled off of the queue and run.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685