Results 1 to 2 of 2

Thread: Python 3 sleep until/interrupts

  1. #1
    Join Date
    Jan 2011
    Location
    the Netherlands
    Beans
    76
    Distro
    Ubuntu 12.04 Precise Pangolin

    Post Python 3 sleep until/interrupts

    Is there a way to let Python (3) sleep (or do something else) until something happens and than call a function. I've read on wikipedia what interrupts are but can't seem to find how to implement them in Python(3). Isn't there something like this in Python:
    Code:
    until <mystatement>:
     <mycode>
    <the code that executes after something happens>
    for example:
    Code:
    until a == 10:
     sleep()
    
    print("An event occured!")
    or something like this:

    Code:
    sleep(until <a_signal>)
    on <a_signal> do:
     <mycode>
    Using a loop and if statements (or only a while loop) causes a very high CPU usage, which of course is completely unnecessary because the program doesn't have to do anything.
    Asus K53SJ-SX216V laptop - Core i3 2310M - 4GB RAM
    - 320GB WD3200 (4KB blocks ) - Intel HD3000/Nvidia Geforce GT520M - Win7 Ho. Pre. 64 bit/Ubuntu 12.04 64 bit

  2. #2
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Python 3 sleep until/interrupts

    sleep() calls the sleep system call via the sleep() C function. It can be interrupted only by sending a signal to the Python process. For example:

    Code:
    firas@itsuki ~ % cat test.py 
    from time import sleep
    try:
            sleep(100)
    except KeyboardInterrupt:
            pass
    print("Waking up.")
    firas@itsuki ~ % python3 test.py 
    Waking up.
    firas@itsuki ~ % 
    ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
    firas@itsuki ~ % ps aux | grep python
    firas     7456  2.0  0.2  12460  5688 pts/9    S+   14:25   0:00 python3 test.py
    firas     7466  0.0  0.0   4372   772 pts/10   S+   14:25   0:00 grep --color=auto python
    firas@itsuki ~ % kill -2 7456
    firas@itsuki ~ %
    (kill -2 sends SIGINT, which is also the signal sent by a press on Ctrl+C, hence why I catch KeyboardInterrupt.)

    A more Pythonic approach would be to use the facilities provided by the Threading module. Basically, create a thread that goes to sleep and have the main thread wake it up. See http://docs.python.org/py3k/library/...#event-objects
    「明後日の夕方には帰ってるからね。」


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •