From mark at markroseman.com Wed Jun 6 14:56:02 2018 From: mark at markroseman.com (Mark Roseman) Date: Wed, 6 Jun 2018 11:56:02 -0700 Subject: [Tkinter-discuss] implementation change to simplify Tkinter event loop and threading issues Message-ID: <46C5583A-F09A-4EDB-B8C3-655F6CEBA8E3@markroseman.com> I?m not sure if this is the right place to discuss internal implementation details, so apologies in advance if not? Ivan and I have been going back and forth regarding some of the subtleties of how Tkinter and Tcl/Tk interact, which can come up particularly with regards to threads, but also in other ways. This is in the context of a reference doc update he?s proposed. It?s gotten me to pop my head into the code (_tkinter.c) several times. It?s a classic case of ?who is in charge? when you try to merge two different event models with two different sets of constraints on them. To simplify, the Tkinter mainloop code is more or less in charge, calling out to Tcl to process one event (or return back if no events show up quickly enough). In the times when it?s not in the middle of a call to Tcl to process an event, it can also call into Tcl directly to run a command (e.g. turn the canvas item pink). Adding threads to the mix is another layer but still amounts to calling into Tcl in the right way at the right time. Tkinter needs to do a lot of clever things with locks, timeouts, etc. to make some of the magic happen. It strikes me there?s another way to do this that might both simplify the Tkinter code and make it more robust, even in some of the weird and wacky ways that people are using it (despite being warned it?s not a good idea!). Tcl?s event handling mechanism was updated in Tcl 7.5 (1996) at the time it was being moved from Unix-only to Mac and Windows. It explicitly provides mechanisms to deal with integrating with other event loops, and situations where either Tcl?s event loop is in charge (but can handle generated events from external code) or vice versa. The documentation can be found at http://www.tcl.tk/man/tcl8.6/TclLib/Notifier.htm Tkinter can potentially make use of these facilities. Rather than the juggling act that Tkinter?s mainloop does now, it can just run Tcl?s event loop. Tkinter would also register itself with Tcl as an event source. This is a way that it can provide new ?events? to the Tcl event loop. In this case, the ?events? are really Tkinter calls that we want to send on to Tcl/Tk. Registering as an event source essentially means that Tcl will repeatedly call into Tkinter to ask ?got anything new for me??. if no Tkinter commands are pending, then the answer is no. If there is a Python command pending, an ?event? can be added to the Tcl event queue. The event handler for that event would then call back into Tkinter, get the Tcl command, execute it, and pass the result back to Tkinter. The best part of this, at least as far as stability goes, is that Tcl controls the timing of the calls into Tkinter, so that it will only make them when it?s ?safe? to do so, from a thread it expects to do so from, etc. Given that Tcl has the stricter restrictions on what can be called when and from where, this seems like a win. On the Tkinter side, it never has to worry about if it?s safe to call into Tcl. (The one exception to this is probably application startup, i.e. before we hit mainloop, where Tkinter would just call directly into Tcl. It may be a reasonable restriction to say that you have to make Tkinter calls from the originating thread if they?re made before you hit mainloop). To me this appears like it should be feasible. Any thoughts? Mark From tjmac at tolisgroup.com Wed Jun 6 15:55:25 2018 From: tjmac at tolisgroup.com (Tim Jones) Date: Wed, 6 Jun 2018 12:55:25 -0700 Subject: [Tkinter-discuss] implementation change to simplify Tkinter event loop and threading issues In-Reply-To: <46C5583A-F09A-4EDB-B8C3-655F6CEBA8E3@markroseman.com> References: <46C5583A-F09A-4EDB-B8C3-655F6CEBA8E3@markroseman.com> Message-ID: > On Jun 6, 2018, at 11:56 AM, Mark Roseman wrote: > > The best part of this, at least as far as stability goes, is that Tcl controls the timing of the calls into Tkinter, so that it will only make them when it?s ?safe? to do so, from a thread it expects to do so from, etc. Given that Tcl has the stricter restrictions on what can be called when and from where, this seems like a win. On the Tkinter side, it never has to worry about if it?s safe to call into Tcl. > > (The one exception to this is probably application startup, i.e. before we hit mainloop, where Tkinter would just call directly into Tcl. It may be a reasonable restriction to say that you have to make Tkinter calls from the originating thread if they?re made before you hit mainloop). > > To me this appears like it should be feasible. Any thoughts? > > Mark Such a change alone would bring me back to Python and TKInter for UI apps. -- Tim From Cameron at phaseit.net Wed Jun 6 19:06:18 2018 From: Cameron at phaseit.net (Cameron Laird) Date: Wed, 6 Jun 2018 23:06:18 +0000 Subject: [Tkinter-discuss] implementation change to simplify Tkinter event loop and threading issues In-Reply-To: References: <46C5583A-F09A-4EDB-B8C3-655F6CEBA8E3@markroseman.com> Message-ID: <20180606230618.GA28343@lairds.us> On Wed, Jun 06, 2018 at 12:55:25PM -0700, Tim Jones wrote: . . . > > On Jun 6, 2018, at 11:56 AM, Mark Roseman wrote: > > > > The best part of this, at least as far as stability goes, is that Tcl controls the timing of the calls into Tkinter, so that it will only make them when it?s ?safe? to do so, from a thread it expects to do so from, etc. Given that Tcl has the stricter restrictions on what can be called when and from where, this seems like a win. On the Tkinter side, it never has to worry about if it?s safe to call into Tcl. > > > > (The one exception to this is probably application startup, i.e. before we hit mainloop, where Tkinter would just call directly into Tcl. It may be a reasonable restriction to say that you have to make Tkinter calls from the originating thread if they?re made before you hit mainloop). > > > > To me this appears like it should be feasible. Any thoughts? > > > > Mark > > Such a change alone would bring me back to Python and TKInter for UI apps. . . . Me, too! That is, this change sounds to me: * feasible * overdue * welcome. I only wish I had availability to offer to help. I actually was working on the Tcl event loop in '96-97 ... From grubert at users.sourceforge.net Sat Jun 9 03:31:27 2018 From: grubert at users.sourceforge.net (engelbert gruber) Date: Sat, 9 Jun 2018 09:31:27 +0200 Subject: [Tkinter-discuss] implementation change to simplify Tkinter event loop and threading issues In-Reply-To: <20180606230618.GA28343@lairds.us> References: <46C5583A-F09A-4EDB-B8C3-655F6CEBA8E3@markroseman.com> <20180606230618.GA28343@lairds.us> Message-ID: does this mean there will be a TIP and a GO ? On 7 June 2018 at 01:06, Cameron Laird wrote: > On Wed, Jun 06, 2018 at 12:55:25PM -0700, Tim Jones wrote: > . > . > . > > > On Jun 6, 2018, at 11:56 AM, Mark Roseman > wrote: > > > > > > The best part of this, at least as far as stability goes, is that Tcl > controls the timing of the calls into Tkinter, so that it will only make > them when it?s ?safe? to do so, from a thread it expects to do so from, > etc. Given that Tcl has the stricter restrictions on what can be called > when and from where, this seems like a win. On the Tkinter side, it never > has to worry about if it?s safe to call into Tcl. > > > > > > (The one exception to this is probably application startup, i.e. > before we hit mainloop, where Tkinter would just call directly into Tcl. It > may be a reasonable restriction to say that you have to make Tkinter calls > from the originating thread if they?re made before you hit mainloop). > > > > > > To me this appears like it should be feasible. Any thoughts? > > > > > > Mark > > > > Such a change alone would bring me back to Python and TKInter for UI > apps. > . > . > . > Me, too! > > That is, this change sounds to me: > * feasible > * overdue > * welcome. > > I only wish I had availability to offer to help. I actually was working > on the Tcl event loop in '96-97 ... > _______________________________________________ > Tkinter-discuss mailing list > Tkinter-discuss at python.org > https://mail.python.org/mailman/listinfo/tkinter-discuss > -------------- next part -------------- An HTML attachment was scrubbed... URL: From grubert at users.sourceforge.net Sat Jun 9 11:38:29 2018 From: grubert at users.sourceforge.net (engelbert gruber) Date: Sat, 9 Jun 2018 17:38:29 +0200 Subject: [Tkinter-discuss] implementation change to simplify Tkinter event loop and threading issues In-Reply-To: References: <46C5583A-F09A-4EDB-B8C3-655F6CEBA8E3@markroseman.com> <20180606230618.GA28343@lairds.us> Message-ID: a PEP of course On 9 June 2018 at 09:31, engelbert gruber wrote: > does this mean there will be a TIP and a GO ? > > On 7 June 2018 at 01:06, Cameron Laird wrote: > >> On Wed, Jun 06, 2018 at 12:55:25PM -0700, Tim Jones wrote: >> . >> . >> . >> > > On Jun 6, 2018, at 11:56 AM, Mark Roseman >> wrote: >> > > >> > > The best part of this, at least as far as stability goes, is that Tcl >> controls the timing of the calls into Tkinter, so that it will only make >> them when it?s ?safe? to do so, from a thread it expects to do so from, >> etc. Given that Tcl has the stricter restrictions on what can be called >> when and from where, this seems like a win. On the Tkinter side, it never >> has to worry about if it?s safe to call into Tcl. >> > > >> > > (The one exception to this is probably application startup, i.e. >> before we hit mainloop, where Tkinter would just call directly into Tcl. It >> may be a reasonable restriction to say that you have to make Tkinter calls >> from the originating thread if they?re made before you hit mainloop). >> > > >> > > To me this appears like it should be feasible. Any thoughts? >> > > >> > > Mark >> > >> > Such a change alone would bring me back to Python and TKInter for UI >> apps. >> . >> . >> . >> Me, too! >> >> That is, this change sounds to me: >> * feasible >> * overdue >> * welcome. >> >> I only wish I had availability to offer to help. I actually was working >> on the Tcl event loop in '96-97 ... >> _______________________________________________ >> Tkinter-discuss mailing list >> Tkinter-discuss at python.org >> https://mail.python.org/mailman/listinfo/tkinter-discuss >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: