它是一个坏主意,在服务器进程创建工作线程? [英] Is it a bad idea to create worker threads in a server process?

查看:153
本文介绍了它是一个坏主意,在服务器进程创建工作线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器进程基本上是响应REST请求的API。

My server process is basically an API that responds to REST requests.

有些要求是启动长时间运行的任务。

Some of these requests are for starting long running tasks.

这是个坏主意,做这样的事情?

Is it a bad idea to do something like this?

get "/crawl_the_web" do
  Thread.new do
    Crawler.new # this will take many many days to complete
  end
end

get "/status" do
  "going well" # this can be run while there are active Crawler threads
end

服务器将不处理超过每天1000个请求。

The server won't be handling more than 1000 requests a day.

推荐答案

不是最好的主意......

Not the best idea....

使用后台作业亚军运行作业。

Use a background job runner to run jobs.

POST / crawl_the_web 应该只是一个作业添加到作业队列中。后台作业亚军将定期检查队列新的工作岗位,才能执行。

POST /crawl_the_web should simply add a job to the job queue. The background job runner will periodically check for new jobs on the queue and execute them in order.

您可以使用,例如,的delayed_job 为此,建立一个单独的进程来轮询和运行的作业。如果你是在Heroku上,你可以使用的delayed_job的功能在一个单独的后台工作运行的作业/赛道。

You can use, for example, delayed_job for this, setting up a single separate process to poll for and run the jobs. If you are on Heroku, you can use the delayed_job feature to run the jobs in a separate background worker/dyno.

这篇关于它是一个坏主意,在服务器进程创建工作线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆