0

I've seen a few of these questions, but haven't found a real answer yet.

I have an application that launches a gstreamer pipe, and then listens to the data it sends back.

In the example application I based mine one, it ends with this piece of code:

gtk.main()

there is no gtk window, but this piece of code does cause it to keep running. Without it, the program exits.

Now, I have read about constructs using while True:, but they include the sleep command, and if I'm not mistaken that will cause my application to freeze during the time of the sleep so ...

Is there a better way, without using gtk.main()?

2
  • 4
    Please expound on your requirements... Tell us what you want to accomplish, not the means by which you mean to accomplish it. Commented Feb 15, 2013 at 22:00
  • What's wrong with gtk.main()? Commented Feb 15, 2013 at 22:05

2 Answers 2

3

gtk.main() runs an event loop. It doesn't exit, and it doesn't just freeze up doing nothing, because inside it has code kind of like this:

while True:
    timeout = timers.earliest() - datetime.now()
    try:
        message = wait_for_next_gui_message(timeout)
    except TimeoutError:
        handle_any_expired_timers()
    else:
        handle_message(message)

That wait_for_next_gui_message function is a wrapper around different platform-specific functions that wait for X11, WindowServer, the unnamed thing in Windows, etc. to deliver messages like "user clicked your button" or "user hit ctrl-Q".

If you call http.serve_forever() or similar on a twisted, HTTPServer, etc., it's doing exactly the same thing, except it's a wait_for_next_network_message(sources, timeout) function, which wraps something like select.select, where sources is a list of all of your sockets.

If you're listening on a gstreamer pipe, your sources can just be that pipe, and the wait_for_next function just select.select.

Or, of course, you could use a networking framework like twisted.


However, you don't need to design your app this way. If you don't need to wait for multiple sources, you can just block:

while True:
    data = pipe.read()
    handle_data(data)

Just make sure the pipe is not set to nonblocking. If you're not sure, you can use setblocking on a socket, fcntl on a Unix pipe, or something I can't remember off the top of my head on a Windows pipe to make sure.

In fact, even if you need to wait for multiple sources, you can do this, by putting a blocking loop for each source into a separate thread (or process). This won't work for thousands of sockets (although you can use greenlets instead of threads for that case), but it's fine for 3, or 30.

Sign up to request clarification or add additional context in comments.

Comments

1

I've become a fan of the Cmd class. It gives you a shell prompt for your programs and will stay in the loop while waiting for input. Here's the link to the docs. It might do what you want.

1 Comment

I always forget about cmd, until right after I've built 70% of it myself (about once every year or two), so thanks for the reminder! However, his program needs to listen to the data on a pipe, and cmd isn't going to help with that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.