在 django wsgi 应用程序中禁用线程
Disable threading in django wsgi applciation
我正在部署一个使用信号的 Django 应用程序(因此它需要 运行 在单个线程中)。我可以使用
在开发服务器中禁用线程
python manage.py runserver --nothreading --noreload
而且效果很好。为了部署应用程序,我遵循 django uwsgi deployment guide 并使用 uwsgi 进行设置。也就是说,
uwsgi ... -module myapp.wsgi
当 运行像这样连接应用程序时,如何禁用 uwsgi 线程?
编辑:
Web 应用程序是在线 Mathematica 解释器 Mathics, e.g. http://mathics.angusgriffith.com/. The signal are used to implement evaluation time limits. We're currently using threads but because of the GIL evaluation can get 'stuck' since everything is CPU bound. We also tried using multiprocessing but the overhead was too high. Link to the Mathics signals code。
要在使用 uwsgi 部署时禁用线程,在您的 uwsgi.ini
文件中将 threads
设置为 1
,这会将脚本限制为 运行 每个单独的 thead进程:
[uwsgi]
processes = 4
threads = 1
因此您可以使用多个进程,每个进程 运行 宁单线程。
考虑到您解释为什么在 Django 应用程序中需要信号,在我看来,您做的事情非常错误。
如果您只是在您的视图中开始计算,那可能需要很长时间,您会在这段时间内冻结整个工作人员。这意味着,worker 无法处理任何其他请求。如果您使用 4 个 worker,并且将有 5 个用户提交需要更多计算的方程式,那么将没有资源来处理来自其中一个的请求,因为所有 4 个 worker 都将忙于计算。
这意味着,对于大量用户,您将需要大量工作人员。每个工人都会消耗资源,你会非常非常快地运行耗尽资源。
换句话说,在视图中 运行 一些长任务是非常糟糕的做法。
考虑使用 celery 之类的任务队列,将每个方程式发送到 celery 中求解。这样,您的工作人员将始终可以处理请求,并且任务将排队。管理冻结的任务也会更容易。
我正在部署一个使用信号的 Django 应用程序(因此它需要 运行 在单个线程中)。我可以使用
在开发服务器中禁用线程python manage.py runserver --nothreading --noreload
而且效果很好。为了部署应用程序,我遵循 django uwsgi deployment guide 并使用 uwsgi 进行设置。也就是说,
uwsgi ... -module myapp.wsgi
当 运行像这样连接应用程序时,如何禁用 uwsgi 线程?
编辑: Web 应用程序是在线 Mathematica 解释器 Mathics, e.g. http://mathics.angusgriffith.com/. The signal are used to implement evaluation time limits. We're currently using threads but because of the GIL evaluation can get 'stuck' since everything is CPU bound. We also tried using multiprocessing but the overhead was too high. Link to the Mathics signals code。
要在使用 uwsgi 部署时禁用线程,在您的 uwsgi.ini
文件中将 threads
设置为 1
,这会将脚本限制为 运行 每个单独的 thead进程:
[uwsgi]
processes = 4
threads = 1
因此您可以使用多个进程,每个进程 运行 宁单线程。
考虑到您解释为什么在 Django 应用程序中需要信号,在我看来,您做的事情非常错误。
如果您只是在您的视图中开始计算,那可能需要很长时间,您会在这段时间内冻结整个工作人员。这意味着,worker 无法处理任何其他请求。如果您使用 4 个 worker,并且将有 5 个用户提交需要更多计算的方程式,那么将没有资源来处理来自其中一个的请求,因为所有 4 个 worker 都将忙于计算。
这意味着,对于大量用户,您将需要大量工作人员。每个工人都会消耗资源,你会非常非常快地运行耗尽资源。
换句话说,在视图中 运行 一些长任务是非常糟糕的做法。
考虑使用 celery 之类的任务队列,将每个方程式发送到 celery 中求解。这样,您的工作人员将始终可以处理请求,并且任务将排队。管理冻结的任务也会更容易。