how can I use uwsgi web.py to run background function?

自古美人都是妖i 提交于 2019-12-05 05:34:36

问题


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)

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.

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.


回答1:


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.



来源:https://stackoverflow.com/questions/15308718/how-can-i-use-uwsgi-web-py-to-run-background-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!