4

I need a simple example of invoking the cherrypy.process.plugins.BackgroundTask.

I tried it out but can't seem to get it to work (no examples in the docs).

Here is my code:

def func():
   print "blah blah blah"
wd = cherrypy.process.plugins.BackgroundTask(15000,func)
wd.run()
insumity
  • 5,311
  • 8
  • 36
  • 64
michael
  • 2,577
  • 5
  • 39
  • 62
  • Here is close to real-world example of `BackgroundTask` usage: http://stackoverflow.com/a/27489635/2072035 – saaj Dec 15 '14 at 17:30

1 Answers1

3

The short answer is, you want to call wd.start(), not wd.run().

Also, because BackgroundTask is daemonic, unless you are doing something else to keep the interpreter alive, Python will exit while your thread floats in the background with no way to see the output.

That said, I've been futzing around with trying to make a working example and haven't succeeded yet. This is the code I am using, which may suck:

import cherrypy.process.plugins

def func():
   print "blah blah blah"
wd = cherrypy.process.plugins.BackgroundTask(15, func)
wd.start()

raw_input()  # hit return when you are bored

wd.cancel()

Finally, looking at the source of BackgroundTask, I see what appears to be a bug -- the exception handler relies on a self.bus attribute that doesn't exist (bus is explicitly set in the other plugins' constructors, but not this class). I don't think that bug is related to my failure to get this working.

marcusds
  • 784
  • 1
  • 7
  • 26
Mike C
  • 1,224
  • 10
  • 26
  • you missed something - BackgroundTask takes seconds as the argument, not milliseconds. I just figured that out!!!! But you get the check mark for the start-not-run plus other details! Thanks. – michael Jun 15 '11 at 21:22
  • @marcusds, fixing the '15000' value makes the code correct, but breaks the conversation: I said it wasn't working in my description, and michael explained why. Your fix makes that conversation nonsensical, and requires me to add another comment to point that out. Context does count. – Mike C Mar 31 '15 at 19:14