Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tcl event loop callback woes #35004

Closed
egnor mannequin opened this issue Aug 19, 2001 · 6 comments
Closed

Tcl event loop callback woes #35004

egnor mannequin opened this issue Aug 19, 2001 · 6 comments

Comments

@egnor
Copy link
Mannequin

egnor mannequin commented Aug 19, 2001

BPO 452973
Nosy @gvanrossum, @loewis, @warsaw, @tiran

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2008-01-05.17:45:11.963>
created_at = <Date 2001-08-19.16:34:31.000>
labels = ['expert-tkinter']
title = 'Tcl event loop callback woes'
updated_at = <Date 2008-01-05.17:45:11.937>
user = 'https://bugs.python.org/egnor'

bugs.python.org fields:

activity = <Date 2008-01-05.17:45:11.937>
actor = 'christian.heimes'
assignee = 'none'
closed = True
closed_date = <Date 2008-01-05.17:45:11.963>
closer = 'christian.heimes'
components = ['Tkinter']
creation = <Date 2001-08-19.16:34:31.000>
creator = 'egnor'
dependencies = []
files = []
hgrepos = []
issue_num = 452973
keywords = []
message_count = 6.0
messages = ['6059', '6060', '6061', '6062', '6063', '59290']
nosy_count = 5.0
nosy_names = ['gvanrossum', 'loewis', 'barry', 'egnor', 'christian.heimes']
pr_nums = []
priority = 'normal'
resolution = 'out of date'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue452973'
versions = []

@egnor
Copy link
Mannequin Author

egnor mannequin commented Aug 19, 2001

I have C code which handles I/O and needs an event
dispatcher. It is running in the context of a Python
GUI application, and uses the Tcl event loop, using
Tcl_CreateFileHandler and friends. The C code gets
callbacks from the Tcl event loop fine, but when it
attempts to call into the Python application from one
of these callbacks, things go wrong.

  • Tkinter has released the GIL and hidden the current
    thread state. I can work around this by re-acquiring
    the GIL from the callback and creating a new thread state.

  • When the callback is invoked, Tkinter's tcl_lock is
    held. If the Python code invoked from the callback
    ultimately calls some other Tkinter function, the
    tcl_lock is still held, and deadlock results. The only
    way to work around this is to use a single-threaded
    Python build.

  • If the Python code returns an error, there's no way
    to stop the event loop to report the error up.
    Tkinter's error-reporting mechanisms are inaccessible.

In general, Tkinter has a lot of infrastructure for
managing callbacks from the Tcl event loop. If a third
party C library wants to use the same event loop, that
infrastructure is unavailable, and it is very difficult
to work with Python.

Unfortunately, short of using threads (which have their
own problems), there's no other alternative for an
external C library to do I/O without blocking the GUI.
I've seen several problem reports from people trying
to do exactly this, though they almost never figure out
all of what's going on, and nobody else ever has any
good advice to offer.

@egnor egnor mannequin added topic-tkinter labels Aug 19, 2001
@warsaw
Copy link
Member

warsaw commented Aug 20, 2001

Logged In: YES
user_id=12800

For lack of a volunteer or better victim... er, assigning to /F

@gvanrossum
Copy link
Member

Logged In: YES
user_id=6380

Unassigning -- /F is a black hole. :-(

@loewis
Copy link
Mannequin

loewis mannequin commented Feb 24, 2002

Logged In: YES
user_id=21627

It seems that all you need to have is access to the Tcl
lock. Would that solve your problem?

As for reporting the error up: This is certainly possible.
Just implement a _report_exception method on your widget;
define it as

    def _report_exception(self):
        raise

@egnor
Copy link
Mannequin Author

egnor mannequin commented Feb 24, 2002

Logged In: YES
user_id=128950

I *think* external access to the tcl_lock would do it (it's
been a while). There are a bunch of helper functions/macros
inside the Tkinter code for handling this situation;
exposing and documenting those would be ideal.

As far as error reporting, I don't think that suffices.
(Again, it's been a while.) The problem is that the
exception is returned to the C side, which must figure out
what to do with it. Specifically, the Tcl event loop should
be stopped and the exception reported to whoever invoked
it... but there's no direct way to stop the Tcl event loop.
(Fuzzy memories here.)

@tiran
Copy link
Member

tiran commented Jan 5, 2008

The bug report is several years old and probably outdated. Please create
a new issue if you are still interested in the problem.

@tiran tiran closed this as completed Jan 5, 2008
@tiran tiran closed this as completed Jan 5, 2008
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants