Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

move web.background to experimental/background.py

  • Loading branch information...
commit 8754812d2c7e2532594c955a5a6d9df52b5d779c 1 parent 2cecae4
@anandology anandology authored
Showing with 43 additions and 40 deletions.
  1. +43 −0 experimental/background.py
  2. +0 −40 web/http.py
View
43 experimental/background.py
@@ -0,0 +1,43 @@
+"""Helpers functions to run log-running tasks."""
+from web import utils
+from web import webapi as web
+
+def background(func):
+ """A function decorator to run a long-running function as a background thread."""
+ def internal(*a, **kw):
+ web.data() # cache it
+
+ tmpctx = web._context[threading.currentThread()]
+ web._context[threading.currentThread()] = utils.storage(web.ctx.copy())
+
+ 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 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
+
View
40 web/http.py
@@ -8,7 +8,6 @@
"prefixurl", "modified",
"write",
"changequery", "url",
- "background", "backgrounder",
"profiler",
]
@@ -126,45 +125,6 @@ def url(path=None, **kw):
return out
-def background(func):
- """A function decorator to run a long-running function as a background thread."""
- def internal(*a, **kw):
- web.data() # cache it
-
- tmpctx = web._context[threading.currentThread()]
- web._context[threading.currentThread()] = utils.storage(web.ctx.copy())
-
- 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 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
-
def profiler(app):
"""Outputs basic profiling information at the bottom of each response."""
from utils import profile
Please sign in to comment.
Something went wrong with that request. Please try again.