如果我想在Web应用程序中使用本机代码进行分析,我该怎么办?

发布于 2024-10-03 23:31:41 字数 467 浏览 0 评论 0原文

问题来了...我想要用户上传一张照片,用户上传照片后,我会有一些处理,来分析这张照片的颜色是什么。但我不想在 php 上这样做......因为我有一个 C 程序可以运行得更快。另外,我不希望用户等待结果。当计算机分析它并得到用户想要的结果时,我会通知用户。我该如何实施?有什么想法或建议吗?谢谢。

我对此的想法是:

  1. 在数据库中写入一条记录,例如用户上传成功时 file001.jpg,也做一个状态,用于 示例,状态:分析
  2. C 程序总是检查是否有新上传 数据库
  3. 如果有新上传,获取file001.jpg
  4. C程序分析完成后,改变状态 数据库,例如:完成
  5. php程序使用ajax,不断拉取数据库,检查 状态,如果状态更改为 “完成”,提示并告诉用户。

您对这个实施有何看法?

Here is the problem...I want the user upload a photo, and after the user uploaded the photo, I will have some processes, to analysis what is the color of this photos. But I don't want to do it on the php... ...because I have a C program that can run it faster. Also, I don't want the user wait for the result. I will notify the user, when the computer analysis it, and have the result the user want. How can I implement it? any ideas or suggestion? Thank you.

My idea on that:

  1. Write a record in database, when user upload success, for example
    file001.jpg, also make a status, for
    example, status: analysis
  2. The C program always check whether it have new upload from
    database
  3. If have a new upload, get the file001.jpg
  4. After the C program finish analysis, change the status in
    database , for example: finish
  5. The php program using ajax, keep pulling the database, checking
    the status, if the status changed to
    "finish", prompt and tell the user.

What do you think about this implementation?

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

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

发布评论

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

评论(3

伴随着你 2024-10-10 23:31:41

你的方法是完全有效的。本质上,您正在创建一个排队机制来将分析与上传分离。您的 Web 应用程序验证上传,然后返回给用户,并将条​​目写入数据库中的队列。

分析需要作为持续运行的守护进程运行,或者可能作为每 x 分钟/秒启动的 cron 作业运行。它会按照设定的时间间隔查看作业队列,看看是否有工作要做,如果有,它就会处理它。完成后,您的守护进程会更新数据库以将作业标记为已完成,并且可能会将与分析相关的新数据写入不同的表中。

不要忘记,如果后台有多个处理守护进程,您还需要将作业标记为“已启动”,以便第二个进程不会重复工作。

当用户返回页面时,会反映新的状态。这可以按照您的建议通过 Ajax 轮询“实时”完成,或者您可以考虑使用 Comet 方法保持实时连接打开 (http://en.wikipedia.org/wiki/Comet_%28programming%29)。

Your approach is perfectly valid. Essentially you are creating a queuing mechanism to decouple the analysis from the upload. Your web app validates the upload and then returns to the user, having written an entry to a queue in a DB.

The analysis needs to run as a daemon that is running continually, or possibly as a cron job that starts every x minutes/seconds. On a set interval it looks at the job queue to see if there is work to be done, if there is, it handles it. On completion, your daemon updates the database to flag the job as completed, and probably writes new data relating to the analysis into a different table.

Don't forget that if you have multiple processing daemons in the background that you also need to flag a job as 'started' so that a second process doesn't end up duplicating the work.

When the user returns to the page, the new status is reflected. This can be accomplished 'live' with an Ajax poll as you suggest, or you could look at keeping a live connection open using a Comet approach (http://en.wikipedia.org/wiki/Comet_%28programming%29).

山有枢 2024-10-10 23:31:41

如果您不想使用 ImageMagick 或 GD(除非这些照片很大,否则您确实可以使用 ImageMagick 或 GD),那么您走在正确的道路上。如果 C 程序足够快,您可以像 Pekka 所说的那样,使用 exec() 在初始线程中立即运行 C 程序。

我最近就这样做了,检查照片的整体颜色以用于照片马赛克。我们和 GD 一起做了这个,而且非常简单。我们通过重新采样将图像缩小到 1 像素,然后从中获取颜色。虽然不是 100% 完美,但对于我们的用途来说已经相当不错了。

If you don't want to use ImageMagick or GD, which you really could unless these photos are HUGE, you are on the right track. If the C program is fast enough, you can do like Pekka says and use exec() to run the C proggy immediately in the initial thread.

I've recently done just this, checking a photo for an over-all color for use in photo-mosaics. We did this with GD and it was pretty straight forward. We shrunk the image down to 1 pixel with resample and then took the color from that. Not 100% perfect, but pretty good for what we are using it for.

与君绝 2024-10-10 23:31:41

Gearman [ http://gearman.org/ ] 可能对您有用。

Gearman 提供了一个通用的应用程序框架,将工作外包给更适合完成该工作的其他机器或流程。它允许您并行工作、负载平衡处理以及在语言之间调用函数。

它支持 C 和 PHP 等语言。您可以作为 Gearman 工作人员用 C 语言编写照片处理脚本。当 PHP 收到文件上传时,您可以告诉 Gearman 守护进程处理您的照片。然后,Gearman 守护进程将选择一个可用的工作人员来完成工作,并在工作完成时通知您。

它还会加快页面加载时间,因为客户端无需等待照片处理完成即可将数据发送到浏览器。

Gearman [ http://gearman.org/ ] may be of use to you.

Gearman provides a generic application framework to farm out work to other machines or processes that are better suited to do the work. It allows you to do work in parallel, to load balance processing, and to call functions between languages.

It supports C and PHP, among other languages. You can write your photo processing script in C as a Gearman worker. When PHP receives a file upload you can then tell the Gearman daemon to process your photo. The Gearman daemon will then pick an available worker to do the work and notify you when you work is complete.

It will also speed up your page loading times as the client will not have to wait for the photo processing to be completed before having data sent to their browser.

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