Try with this code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#from web import run, background, backgrounder
import web
from datetime import datetime; now = datetime.now
from time import sleep
from background import background, backgrounder
from webutil.webmisc import debug
urls = (
'/', 'index',
)
web.config.debug = True
app = web.application(urls, globals())
app.internalerror = web.debugerror
form_main = """$def with (o)
<HTML>
<HEAD>
<STYLE>
@import '/static/skin/default.css';
</STYLE>
</HEAD>
<BODY>
<CENTER>
<H3>Background runner</H3>
<P/>
<P/>
<IFRAME src='/static/out.html' width='50%' height='70%'>
</IFRAME>
</BODY>
</HTML>
"""
class index:
def GET(self):
o = web.Storage
self.startthread()
t = web.template.Template(form_main)
return t(o)
@backgrounder
def startthread(self):
longrunning()
@background
def longrunning():
open('./static/out.html','wt').write("""<HTML><HEAD>
<META HTTP-EQUIV=Pragma CONTENT=no-cache>
<META HTTP-EQUIV=Refresh CONTENT=1>
</HEAD><BODY>Background thread is running...<BR>""")
for i in range(10):
sleep(3)
open('./static/out.html', 'at').write("%s: %s<BR>" % (i, now
()) )
if __name__ == '__main__':
app.run()
On Jan 3, 8:54 am, Alex K <[email protected]> wrote:
> I made these modifications and here is what I got while running the
> cookbook example:
>
> Started at 2009-01-03 00:20:03.642114
> hit f5 to refresh!
> 303 See Other
>
> In the cookbook I replaced every print with yield.
>
> Did anyone manage to make web.background work with web 0.3?
>
> Thank you.
>
> On Dec 16 2008, 12:22 pm, leone <[email protected]> wrote:
>
> > It runs with these updates:
> > """Helpers functions to run log-running tasks."""
> > -from web import utils
> > +from web import utils, http
> > from web import webapi as web
> > +import threading
>
> > defbackground(func):
> > """A function decorator to run a long-running function as
> > abackgroundthread."""
> > def internal(*a, **kw):
> > web.data() # cache it
>
> > + web._context = {}
> > - tmpctx = web._context[threading.currentThread()]
> > web._context[threading.currentThread()] = utils.storage
> > (web.ctx.copy())
> > + tmpctx = web._context[threading.currentThread()]
>
> > def newfunc():
> > web._context[threading.currentThread()] = tmpctx
> > func(*a, **kw)
> > myctx = web._context[threading.currentThread()]
> > for k in myctx.keys():
> > if k not in ['status', 'headers', 'output']:
> > try: del myctx[k]
> > except KeyError: pass
>
> > t = threading.Thread(target=newfunc)
> > background.threaddb[id(t)] = t
> > t.start()
> > web.ctx.headers = []
> > - return seeother(changequery(_t=id(t)))
> > + return web.seeother(http.changequery(_t=id(t)))
> > return internal
> > background.threaddb = {}
>
> > def backgrounder(func):
> > def internal(*a, **kw):
> > i = web.input(_method='get')
> > if '_t' in i:
> > try:
> > t =background.threaddb[int(i._t)]
> > except KeyError:
> > return web.notfound()
> > web._context[threading.currentThread()] = web._context[t]
> > return
> > else:
> > return func(*a, **kw)
> > return internal
>
> > Theexamplemust be changed.
> > I used an iframe and a global var.
>
> > On Dec 16, 2:32 am, "Anand Chitipothu" <[email protected]> wrote:
>
> > > On Tue, Dec 16, 2008 at 5:55 AM, Aaron Swartz <[email protected]> wrote:
>
> > > >> How can I suggest you some fix tobackgroundmodule?
>
> > > > Sure, what's wrong with ti?
>
> > > It doesn't work with 0.3 and it has been moved to experimental.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web.py" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/webpy?hl=en
-~----------~----~----~----~------~----~------~--~---