Sunday, 15 September 2013

Do not exit python program, but keep running -



Do not exit python program, but keep running -

i've seen few of these questions, haven't found real reply yet.

i have application launches gstreamer pipe, , listens info sends back.

in illustration application based mine one, ends piece of code:

gtk.main()

there no gtk window, piece of code cause maintain running. without it, programme exits.

now, have read constructs using while true:, include sleep command, , if i'm not mistaken cause application freeze during time of sleep ...

is there improve way, without using gtk.main()?

gtk.main() runs event loop. doesn't exit, , doesn't freeze doing nothing, because within has code kind of 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 wrapper around different platform-specific functions wait x11, windowserver, unnamed thing in windows, etc. deliver messages "user clicked button" or "user nail ctrl-q".

if phone call http.serve_forever() or similar on twisted, httpserver, etc., it's doing same thing, except it's wait_for_next_network_message(sources, timeout) function, wraps select.select, sources list of of sockets.

if you're listening on gstreamer pipe, sources can pipe, , wait_for_next function select.select.

or, of course, utilize networking framework twisted.

however, don't need design app way. if don't need wait multiple sources, can block:

while true: info = pipe.read() handle_data(data)

just create sure pipe not set nonblocking. if you're not sure, can utilize setblocking on socket, fcntl on unix pipe, or can't remember off top of head on windows pipe create sure.

in fact, if need wait multiple sources, can this, putting blocking loop each source separate thread (or process). won't work thousands of sockets (although can utilize greenlets instead of threads case), it's fine 3, or 30.

python

No comments:

Post a Comment