Code Monkey home page Code Monkey logo

Comments (6)

 avatar commented on August 11, 2024

Logged In: YES
user_id=14198

we talked about SetConsoleCtrlHandler a while ago.

>Sorry for the delay, but yes, that sounds like a reasonable
> approach. The only problem is with the "last-registered,
> first-called" approach, meaning second calls to your
> extensions function would not have the same effect as a
> second call to the real SetConsoleCtrlHandler - but yeah,
> I see no reasonable solution worth the effort.
>
>The GIL management will not be trivial, and may require
> you to get - err - creative :)

The GIL wasn't too hard in the end - the handler is called
in the context
of a new thread, and there is precedent for that in the
source :)

I have attached a simplistic version of a
SetConsoleCtrlHandler. It allows
to set *one* handler function and doesn't implement the list
I originally
had in mind.

The code works works, but its behaviour is far from the
behaviour of
SetConsoleCtrlHandler in a C program.

This is probably due to the signal handling of Python
itself, and I can't
see what can be done about it.

The current code is useful to me, and it could be improved
to handle
multiple handlers properly (by installing different C
handlers, thereby
preserving the order), but it would be rather hard to
document the
behaviour, since it clashes with the Python internal signal
handling.

For example, take:

import cch # this is the console controle handler module
import time

def handler(type):
print type
return False

cch.SetConsoleCtrlHandler(handler, True)

time.sleep(60)

If you execute this, and hit Ctrl-C before the 60 seconds
are up, the
handler will dutifully print 0, but the process won't be
killed - because
time.sleep() cannot be killed in Python.

If you do a sys.exit() in the handler function, it will also
not terminate
the process, since sys.exit() only works from the main thread.

Basically, I have no clue how the Python
SetConsoleCtrlHandler should be
documented. It would need a link to description of standard
Python signal
handling to be generally useful, but I am not aware of any
such documentation.

Original comment by: mhammond

from pywin32.

 avatar commented on August 11, 2024

from lars

Original comment by: mhammond

from pywin32.

 avatar commented on August 11, 2024

from lars

Original comment by: mhammond

from pywin32.

 avatar commented on August 11, 2024

from lars

Original comment by: mhammond

from pywin32.

 avatar commented on August 11, 2024

SetConsoleCtrlHandler was added to win32api module on 10.10.2004. Can this feature request be closed?

Original comment by: zseil

from pywin32.

 avatar commented on August 11, 2024
  • status: open --> closed-fixed

Original comment by: mhammond

from pywin32.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.