3

I have an existing Python class that is used for data capture. It has start() and stop() methods to start and stop the data capture. Right now this class is used in a single Python script, e.g.:

dataGrabber = DataGrabber()
dataGrabber.start()
# do a bunch of stuff...
dataGrabber.stop()

We want to break out the start and stop functionality and be able to start and stop the data capture from the command line, e.g.:

python CaptureData.py start
<<< go off and do something else >>>
python CaptureData.py stop

I need a way for CaptureData.py to create an instance of the DataGrabber class when the script is called with the "start" argument, save that class instance somehow, and have it be accessible when I call the script with the "stop" argument.

I tried using shelve, but the DataGrabber class has tons of instance methods, and Python can't pickle instance methods.

Any ideas? Is there any way I can do this?

  • Is execution supposed to continue in between command-line calls? – Ken Kinder Jan 24 '12 at 19:10
  • Yes. The start() method starts data capture. It talks to an embedded system to gather data. This needs to run until we stop it, externally from the command line. – user1167666 Jan 24 '12 at 20:03
  • Use shelve anyway :) http://stackoverflow.com/a/1798869/16361 – AdamKG Jan 24 '12 at 18:45
  • Thanks a lot. This helped me get past being unable to pickle an instance method. However, the existing DataGrabber class has lots of other things that can't be pickled, apparently. Thread Lock, FileType, etc. objects. Is there anyway to do what I need to do without shelve and pickle? Such as... creating a subprocess when calling the script with the "start" argument, that I then pass some type of signal to when calling the script with the "stop" argument. This is on Windows, for what it's worth. Grasping at straws here... – user1167666 Jan 24 '12 at 19:47
  • Yup... have the start part write the PID to a file & [listen for a signal](http://docs.python.org/library/signal.html), and the stop part read the PID & send the signal to that process. It's technically a daemon at that point; what I described is just a bare-bones implementation of one. – AdamKG Jan 25 '12 at 19:01

1 Answers1

3

It sounds like you want to create a daemon. Depending on your OS, you should be able to use python-daemon to do this.

Edit: Since you say you're using Windows, can you instead run your script as a service?

This question and its corresponding answer seem like they are very relevant. The answer even makes it look simple!

Community
  • 1
  • 1
aganders3
  • 5,838
  • 26
  • 30