commit-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Commit-gnuradio] r8191 - in gnuradio/trunk: gnuradio-core/src/python/gn


From: jcorgan
Subject: [Commit-gnuradio] r8191 - in gnuradio/trunk: gnuradio-core/src/python/gnuradio/gruimpl gnuradio-examples/python/audio
Date: Sat, 12 Apr 2008 12:36:17 -0600 (MDT)

Author: jcorgan
Date: 2008-04-12 12:36:16 -0600 (Sat, 12 Apr 2008)
New Revision: 8191

Added:
   gnuradio/trunk/gnuradio-core/src/python/gnuradio/gruimpl/daemon.py
   gnuradio/trunk/gnuradio-examples/python/audio/dial_tone_daemon.py
Modified:
   gnuradio/trunk/gnuradio-core/src/python/gnuradio/gruimpl/Makefile.am
   gnuradio/trunk/gnuradio-examples/python/audio/Makefile.am
Log:
Adds gru.daemonize() and example application.  Simplifies running GNU Radio 
applications as background daemon processes instead of foreground applications.

Modified: gnuradio/trunk/gnuradio-core/src/python/gnuradio/gruimpl/Makefile.am
===================================================================
--- gnuradio/trunk/gnuradio-core/src/python/gnuradio/gruimpl/Makefile.am        
2008-04-12 17:42:20 UTC (rev 8190)
+++ gnuradio/trunk/gnuradio-core/src/python/gnuradio/gruimpl/Makefile.am        
2008-04-12 18:36:16 UTC (rev 8191)
@@ -35,6 +35,7 @@
        os_read_exactly.py              \
        sdr_1000.py                     \
        seq_with_cursor.py              \
-       socket_stuff.py                 
+       socket_stuff.py                 \
+       daemon.py
 
 CLEANFILES = *.pyc

Added: gnuradio/trunk/gnuradio-core/src/python/gnuradio/gruimpl/daemon.py
===================================================================
--- gnuradio/trunk/gnuradio-core/src/python/gnuradio/gruimpl/daemon.py          
                (rev 0)
+++ gnuradio/trunk/gnuradio-core/src/python/gnuradio/gruimpl/daemon.py  
2008-04-12 18:36:16 UTC (rev 8191)
@@ -0,0 +1,102 @@
+#
+# Copyright 2008 Free Software Foundation, Inc.
+# 
+# This file is part of GNU Radio
+# 
+# GNU Radio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+# 
+# GNU Radio is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with GNU Radio; see the file COPYING.  If not, write to
+# the Free Software Foundation, Inc., 51 Franklin Street,
+# Boston, MA 02110-1301, USA.
+# 
+import os, sys, signal
+
+# Turn application into a background daemon process.
+#
+# When this function returns:
+#
+# 1) The calling process is disconnected from its controlling terminal
+#    and will not exit when the controlling session exits
+# 2) If a pidfile name is provided, it is created and the new pid is
+#    written into it.
+# 3) If a logfile name is provided, it is opened and stdout/stderr are
+#    redirected to it.
+# 4) The process current working directory is changed to '/' to avoid
+#    pinning any filesystem mounts.
+# 5) The process umask is set to 0111.
+#
+# The return value is the new pid.
+#
+# To create GNU Radio applications that operate as daemons, add a call to this
+# function after all initialization but just before calling gr.top_block.run()
+# or .start().
+#
+# Daemonized GNU Radio applications may be stopped by sending them a
+# SIGINT, SIGKILL, or SIGTERM, e.g., using 'kill pid' from the command line.
+#
+# If your application uses gr.top_block.run(), the flowgraph will be stopped
+# and the function will return.  You should allow your daemon program to exit
+# at this point.
+#
+# If your application uses gr.top_block.start(), you are responsible for 
hooking
+# the Python signal handler (see 'signal' module) and calling 
gr.top_block.stop()
+# on your top block, and otherwise causing your daemon process to exit.
+#
+
+def daemonize(pidfile=None, logfile=None):
+    # fork() into background
+    try:
+       pid = os.fork()
+    except OSError, e:
+       raise Exception, "%s [%d]" % (e.strerror, e.errno)
+
+    if pid == 0:       # First child of first fork()
+       # Become session leader of new session
+       os.setsid()
+       
+       # fork() into background again
+       try:
+           pid = os.fork()
+       except OSError, e:
+           raise Exception, "%s [%d]" % (e.strerror, e.errno)
+
+       if pid != 0:
+           os._exit(0) # Second child of second fork()
+
+    else:              # Second child of first fork()
+       os._exit(0)
+       
+    os.umask(0111)
+
+    # Write pid
+    pid = os.getpid()
+    if pidfile is not None:
+       open(pidfile, 'w').write('%d\n'%pid)
+       
+    # Redirect streams
+    if logfile is not None:
+       lf = open(logfile, 'a+')
+       sys.stdout = lf
+       sys.stderr = lf
+
+    # Prevent pinning any filesystem mounts
+    os.chdir('/')
+
+    # Tell caller what pid to send future signals to
+    return pid
+
+if __name__ == "__main__":
+    import time
+    daemonize()
+    print "Hello, world, from daemon process."
+    time.sleep(20)
+    print "Goodbye, world, from daemon process."

Modified: gnuradio/trunk/gnuradio-examples/python/audio/Makefile.am
===================================================================
--- gnuradio/trunk/gnuradio-examples/python/audio/Makefile.am   2008-04-12 
17:42:20 UTC (rev 8190)
+++ gnuradio/trunk/gnuradio-examples/python/audio/Makefile.am   2008-04-12 
18:36:16 UTC (rev 8191)
@@ -27,6 +27,7 @@
        audio_play.py           \
        audio_to_file.py        \
        dial_tone.py            \
+       dial_tone_daemon.py     \
        dial_tone_wav.py        \
        mono_tone.py            \
        multi_tone.py           \

Added: gnuradio/trunk/gnuradio-examples/python/audio/dial_tone_daemon.py
===================================================================
--- gnuradio/trunk/gnuradio-examples/python/audio/dial_tone_daemon.py           
                (rev 0)
+++ gnuradio/trunk/gnuradio-examples/python/audio/dial_tone_daemon.py   
2008-04-12 18:36:16 UTC (rev 8191)
@@ -0,0 +1,57 @@
+#!/usr/bin/env python
+#
+# Copyright 2004,2005,2007,2008 Free Software Foundation, Inc.
+# 
+# This file is part of GNU Radio
+# 
+# GNU Radio is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+# 
+# GNU Radio is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with GNU Radio; see the file COPYING.  If not, write to
+# the Free Software Foundation, Inc., 51 Franklin Street,
+# Boston, MA 02110-1301, USA.
+# 
+
+from gnuradio import gr, gru
+from gnuradio import audio
+from gnuradio.eng_option import eng_option
+from optparse import OptionParser
+import os
+
+class my_top_block(gr.top_block):
+
+    def __init__(self):
+        gr.top_block.__init__(self)
+
+        parser = OptionParser(option_class=eng_option)
+        parser.add_option("-O", "--audio-output", type="string", default="",
+                          help="pcm output device name.  E.g., hw:0,0 or 
/dev/dsp")
+        parser.add_option("-r", "--sample-rate", type="eng_float", 
default=48000,
+                          help="set sample rate to RATE (48000)")
+        (options, args) = parser.parse_args ()
+        if len(args) != 0:
+            parser.print_help()
+            raise SystemExit, 1
+
+        sample_rate = int(options.sample_rate)
+        ampl = 0.1
+
+        src0 = gr.sig_source_f (sample_rate, gr.GR_SIN_WAVE, 350, ampl)
+        src1 = gr.sig_source_f (sample_rate, gr.GR_SIN_WAVE, 440, ampl)
+        dst = audio.sink (sample_rate, options.audio_output)
+        self.connect (src0, (dst, 0))
+        self.connect (src1, (dst, 1))
+
+
+if __name__ == '__main__':
+    pid = gru.daemonize()
+    print "To stop this program, enter 'kill %d'" % pid
+    my_top_block().run()


Property changes on: 
gnuradio/trunk/gnuradio-examples/python/audio/dial_tone_daemon.py
___________________________________________________________________
Name: svn:executable
   + *





reply via email to

[Prev in Thread] Current Thread [Next in Thread]