在 Rails 中分叉一个进程并向其发送数据

发布于 2024-08-27 03:28:58 字数 261 浏览 5 评论 0原文

我正在制作一个 Rails 应用程序。 在一个操作中,我需要生成一个长时间运行的进程。 这不是问题。我可以使用spawn gem 或其他一些方法来分叉新进程。 但在进程产生一段时间后, 用户必须能够将附加数据传递给该进程。

当然,我可以分叉进程来监听 UNIX 套接字, 在 HTTP 会话中存储套接字地址并 当用户需要将新数据传递给进程时,使用 drb 协议与该进程进行通信。但我认为这不是最好的解决方案,将应用程序部署到托管将是一个问题。

有什么简单的方法可以做到这一点?

I'm making a Rails application.
In the one action I need to spawn a long running process.
This is not a problem. I can fork new process using spawn gem or some other.
But some time after process has been spawned,
user must be able to pass additional data to that process.

Sure, I can fork process which will listen a UNIX socket,
store socket address in the HTTP session and
communicate with that process using drb protocol when user will require to pass new data to process. But I think it is not best solution and it will be a problem to deploy an application to the hosting.

What is the easy way to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

明月夜 2024-09-03 03:28:58

附加数据是否可以进入数据库,并且该过程在那里检查“新数据”?

Can the additional data go into the database, and the process checks there for the "new data"?

狼性发作 2024-09-03 03:28:58

我建议上一层楼。使用delayed_job来处理作业参数的生成和初始设置。

您可以修改delayed_job 的dbms 模型来添加您想要稍后发送到作业的附加信息的字段。

那么您的流程将是:

  1. 使用delayed_job提交。存储作业表的 ID。如果 stock de;ayed_job 没有为您提供作业 ID,则根据需要修改delayed_job。

  2. 将附加数据添加到作业表中的字段

如果您的主作业需要从派生作业返回数据,那么您还可以有一个数据库字段那。

您应该尝试设计您的应用程序以最大限度地减少所需的消息传递量。否则你需要设计一个消息协议。可以做,但这是你应该尽量避免的工作。

I suggest going up-level. Use delayed_job to handle the spawning and initial setting of parameters for your job.

You can modify delayed_job's dbms model to add fields for the additional information that you want to later send to the job.

Then your flow would be:

  1. Submit using delayed_job. Store the id of the jobs table. If stock de;ayed_job doesn't give you the job id, then modify delayed_job as necessary.

  2. Add the additional data to the field in the jobs table

If your main job needs data back from the forked job, then you could also have a db fields for that.

You should try to design your application to mimimize the amount of message passing needed. Otherwise you'll need to design a message protocol. Can be done, but it is work that you should try to avoid.

爱本泡沫多脆弱 2024-09-03 03:28:58

难道不能使用一个线程并通过两个队列与其通信,一个用于线程的输入,另一个用于线程的响应吗?

Can't you use a thread and communicate with it via two queues, one for input to the thread, and one for the thread's responses?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文