如何使用uwsgi web.py运行后台功能? [英] how can I use uwsgi web.py to run background function?
问题描述
说有新评论时,我需要给自己发送电子邮件.
而且我不想阻止web.py向浏览器显示HTML.
和线程似乎在这里不起作用.
Say I need to send an email to myself when there is new comment.
and I don't want to block web.py presenting HTML to browser.
and Threading seems don't work here.
class comment:
def POST(self):
...
sender = Thread(target=_sendmail,args=('New Comment',msg))
sender.start()
referer = web.ctx.get('HTTP_REFERER', 'http://www.domain.com')
raise web.SeeOther(referer)
使用线程时的问题是POST函数完成后..其中的发件人将被冻结..通常发件人没有完成其工作. 而且我当然不想使用sender.join()等待发送者结束.
the problem when using threading is that once POST function is finished.. the sender within it would be freezed..usually sender didn't finish its job. And I certainly don't want to use sender.join() to wait for sender to end.
我认为uwsgi与此无关..
但是我看到一个解释建议,当没有请求或请求完成时,uwsgi暂停了web.py应用程序.
web.py提供了一种称为@background的方法. http://webpy.org/cookbook/background
但是似乎有某些问题.它没有清理threaddb字典.
并且会在网址中添加一个参数,例如 http://domain.com:8080/?_t=3080772748 很难看.
有更好的解决方案吗? 在照常提供网址请求的同时发送电子邮件.
I think uwsgi don't have anything to do with this..
but I saw a explanation suggested that uwsgi suspended the web.py app.when there's no request.or request is done.
web.py provide an approach called @background.. http://webpy.org/cookbook/background
But it's seems have certain problems.it doesn't clean up threaddb dictionary.
and it would add an argument to the url like http://domain.com:8080/?_t=3080772748 which is ugly.
Is there an better solution? sending an email while serve url request as usual.
推荐答案
在uWSGI中使用线程没有问题,但是请记住使用--enable-threads启用它们,否则将禁用GIL(出于性能原因)
there is no problem in using threads in uWSGI, but remember to enable them with --enable-threads otherwise the GIL (for performance reasons) it is not enabled.
这篇关于如何使用uwsgi web.py运行后台功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!