PHP/MySQL 中的异步消息传递?

发布于 2024-12-14 03:33:30 字数 1091 浏览 1 评论 0原文

我正在开发图像托管网站(Gallery 的竞争对手),需要提高我的水平。新照片通过 FTP 上传或放置在服务器上并建立索引(快速),然后缩略图(慢速)。

  1. 最简单的实现是允许用户在等待时上传,然后索引和缩略图(网页加载缓慢)。管理员可以登录索引,然后对手动上传的其他文件进行缩略图(页面加载缓慢,刷新可能需要数小时)。

  2. 我现在的实现是用户上传在等待时导致索引,有时(随机)整个站点索引会搭载在正常页面加载之上(给用户带来额外的边际延迟)。网站上的页面引用缩略图所在的 URL(如果存在)。如果用户请求一个不存在的缩略图,则会在等待时创建该缩略图(“惰性缩略图”)。

2的好处是在多核系统上,所有核心都被使用。缺点是,第一次加载新照片页面需要 30x50 MB,并且页面超时,一些图像已完成,其他图像正在处理后续加载。

所以问题是这里实现任务处理的正确方法是什么?如果它可以扩展到共享数据库上的多个服务器,则可以获得奖励积分。

一个想法如下(可能是胡言乱语)

我考虑在数据库中创建一个作业表:

id INT NOT NULL AUTO_INCREMENT, 
priority INT NOT NULL, 
worker INT, 
workstarted DATETIME, 
func CHAR(10) NOT NULL, 
args VARCHAR NOT NULL, 
PRIMARY KEY(id),
INDEX(priority),
UNIQUE(func,args) -- prevent duplicate jobs if user does F5

然后当需要完成工作时,插入一行并创建一个工作线程(如果没有足够的线程)已经存在)。然后等待响应,如果作业成功完成,则继续,否则 5 秒后超时。如果出现超时,请使用 AJAX 在内容可用时将其获取给用户。

我不知道如何用这种方法做的是:如果存在足够的工作线程,如何协调(使用表和心跳?),工作人员如何发送响应(完成后删除作业并写入另一个表)如果失败?),请求者如何有效地等待响应或超时(有什么比轮询数据库更好的方法吗?)。根据这些方法的不同,系统可以根据在不同服务器上运行的工作人员进行扩展。

I'm working on image hosting website (a competitor to Gallery) and need to step up my game. New photos are uploaded or placed on the server via FTP and are indexed (quick), then they are later thumbnailed (slow).

  1. The most simple implementation is to allow users to upload, then index and thumbnail while they wait (slow web page load). An admin can log in to index and then thumbnail other files that were manually uploaded (slow page load that refreshes and can take hours).

  2. The implementation I have now is user uploads cause indexing while they wait, and sometimes (randomly) the whole site indexes to piggyback on top of a normal pageload (cause additional marginal delay for the user). Pages on the site refer to the URL where the thumbnail should be if it existed. If the user requests a nonexistent thumbnail it is created while they wait ("lazy thumbnailing").

The benefit of 2 is that on a multicore system, all cores are used. The downside is that the first time a page of new photos loads 30x50 MB is needed and the page times out, with some images completing and others working on subsequest loads.

SO THE QUESTION IS what is the right way to implement task processing here? Bonus points if it scales to multiple servers on a shared database.

One idea follows (may be drivel):

I thought about making a job table in the DB:

id INT NOT NULL AUTO_INCREMENT, 
priority INT NOT NULL, 
worker INT, 
workstarted DATETIME, 
func CHAR(10) NOT NULL, 
args VARCHAR NOT NULL, 
PRIMARY KEY(id),
INDEX(priority),
UNIQUE(func,args) -- prevent duplicate jobs if user does F5

Then when work needs to get done, insert a row and create a worker thread (if enough threads don't already exist). Then wait for the response and proceed if the job was completed successfully, or timeout after 5 seconds. If there was a timeout, use AJAX to get the content to the user when it is available.

Things I don't know how to do in this approach are: how to coordinate if enough worker threads exist (use a table and a heart beat?), how the worker sends the response (delete the job when complete and write to another table if failed?), how the requestor waits for the response efficiently or times out (anything better than polling the DB?). Depending on the approach to these, the system can scale with workers running on different servers.

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

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

发布评论

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

评论(1

北方的韩爷 2024-12-21 03:33:31

我建议使用 ZeroMQ 来解决您的问题。

它是一个具有多种语言(包括 PHP)绑定的套接字框架。您可以在不使用数据库的情况下开发工作人员系统,并且可以跨多个节点(执行工作的物理机器)扩展工作。

这里介绍了 0MQ

这里是包含 PHP 示例的文档

这是 github 上的 0MQ 指南

主观意见:使用 0MQ 简直太棒了。它的速度令人难以置信,并且有大量的示例,以及出色的指南。

I suggest using ZeroMQ for your problem.

It's a socket framework with bindings in multiple languages (including PHP). You can develop a system of workers without the use of the database, and you can scale the work across multiple nodes (physical machines that do the work).

Here's an introduction to the 0MQ.

Here's the documentation with PHP examples.

Here's the 0MQ guide at github.

Subjective opinion: it simply rocks to use 0MQ. It's incredibly fast and there's tons of examples, combined with an excellent guide.

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