zoukankan      html  css  js  c++  java
  • 用threading 解决 gunicorn worker timeout

      产生worker timeout 的背景

            while 1:

                  .....

                  time.sleep(1)

           gunicorn运行起来,只等待了30s,就卡住了,没报任何异常或err,查了gunicorn 官方文档,原来是线程默认等待30s 就kill 掉,再restart

           http://docs.gunicorn.org/en/stable/settings.html

           

    timeout

    • -t INT, --timeout INT
    • 30

    Workers silent for more than this many seconds are killed and restarted.

    Generally set to thirty seconds. Only set this noticeably higher if you’re sure of the repercussions for sync workers. For the non sync workers it just means that the worker process is still communicating and is not tied to the length of time required to handle a single request.

             根本原因找到了,在gunicorn启动加了--timeout  120 ,还是超过30s 就worker timeout.搜了一圈stack没发现好的解决方法。

            解决这个问题,目前最好的方法,就是在程序改代码,原先是主线程调用,用threading包装一下

            如:

             import threading

             t = threading.Thread(name = '', target = func ,kwargs{})

             t.daemon = True

             t.start()

             

              t = threading.Thread(name='result_package', target=result_package, args=(pack_name, task, issue))

       t.daemon = True
        t.start()

                     

               这样就在主线程下,把方法包装起来。

               顺便用

               Event().wait(15)  替代 time.sleep(16)

         这样写法的好处是不占用cpu,释放!

              

               刚开始,分析原因花了不少时间,几行代码就把worker timeout解决了。之前试了map.thread不行。

              准备用队列(celery+redis)替代原来的逻辑,只是工作量有点大,太重了。

  • 相关阅读:
    PreferenceScreen 偏好显示类 的使用
    Android实战技巧:如何在ScrollView中嵌套ListView
    android onActivityResult不执行问题
    java zip解压中文乱码问题
    RandomAccessFile读取大文件时效率很低,现进行改进---BufferedRandomAccessFile
    HTTP RANGE(多线程下载相关)
    Android实现ListView异步加载图片
    自己调用webservice方法总结(带请求头SoapHeader)
    Android将程序崩溃信息保存本地文件
    Android的intent之间复杂参数的传递
  • 原文地址:https://www.cnblogs.com/ExMan/p/10899823.html
Copyright © 2011-2022 走看看