Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8657127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:26:43+00:00 2026-06-12T15:26:43+00:00

I’m writing a Python extension for the functions provided by a GPIO driver. I

  • 0

I’m writing a Python extension for the functions provided by a GPIO driver. I made progress pretty easily on the simple functions like set_bit() and clear_bit(). But now I need to implement wait_int(), which sleeps until an event is sensed on an input pin and I’m not sure the right way to orchestrate this between c and python. Here’s a stripped down example of using the function in c:

main(int argc, char *argv[])
{
    int c;

    //some setup like testing port availability, clearing interrupts, etc
    ...

    while(1)
    {
        printf("**\n");
        c = wait_int(1);//sleeps until an interrupt occurs on chip 1
        if(c > 0) {
            printf("Event sense occured on Chip 1 bit %d\n",c);
            ++event_count;
        }
        else
            break;
    }

    printf("Event count = %05d\r",event_count);
    printf("\nExiting Now\n");
}

Do I just expose wait_int pretty much directly and then do whatever the python equivalent idiom of the infinite loop is? There’s also some debouncing that needs to be done. I’ve done it in c but maybe it could be moved to the python side.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:26:44+00:00Added an answer on June 12, 2026 at 3:26 pm

    You don’t need to do anything on the Python side, you can just treat it as a synchronous function. On the C side, you just block until the event occurs, possibly allowing interrupts. For example, take a look at the implementation of the time.sleep function:

    /* LICENSE: http://docs.python.org/license.html */
    
    /* Implement floatsleep() for various platforms.
       When interrupted (or when another error occurs), return -1 and
       set an exception; else return 0. */
    
    static int
    floatsleep(double secs)
    {
    /* XXX Should test for MS_WINDOWS first! */
    #if defined(HAVE_SELECT) && !defined(__BEOS__) && !defined(__EMX__)
        struct timeval t;
        double frac;
        frac = fmod(secs, 1.0);
        secs = floor(secs);
        t.tv_sec = (long)secs;
        t.tv_usec = (long)(frac*1000000.0);
        Py_BEGIN_ALLOW_THREADS
        if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t) != 0) {
    #ifdef EINTR
            if (errno != EINTR) {
    #else
            if (1) {
    #endif
                Py_BLOCK_THREADS
                PyErr_SetFromErrno(PyExc_IOError);
                return -1;
            }
        }
        Py_END_ALLOW_THREADS
    #elif defined(__WATCOMC__) && !defined(__QNX__)
        ...
    

    All it does is use the select function to sleep for the given period of time. select is used so that if any signal is received (such as SIGINT from hitting Ctrl+C at the terminal), the system call is interrupted and control returns to Python.

    Hence. your implementation can just call the C wait_int function. If it supports being interrupted by signals, than great, that will allow the user to interrupt it by hitting Ctrl+C, but make sure to react appropriately such that an exception will be thrown (I’m not certain of how this works, but it looks like returning NULL from the top-level function (time_sleep in this example) will do the trick).

    Likewise, for better multithreaded performance, surround the wait call with a pair of Py_BEGIN_ALLOW_THREADS/Py_END_ALLOW_THREADS macros, but this is not required, especially if you’re not using multithreading at all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.