Non blocking functions PHP

后端 未结 7 1592
粉色の甜心
粉色の甜心 2020-12-29 16:12

I have a project in which a user uploads an image through a form and the server does some thumbnails. The thumbnail making process is very slow so I thought that doing the i

相关标签:
7条回答
  • 2020-12-29 17:03

    On one system, I've seen an independent background process making the thumbnails:

    • form is processed normally, without generating any thumbnail at all
    • the image is given an unique name and copied to a special folder.
    • a database entry is created in a thumbnails table, linking the original image and the new unique name, marked as "to be thumbnailed"
    • the form processing script stops to care and continues with whatever else it needs to do.

    There's an independent background process (and its watchdog), which continuously watches that special folder (most OSes have various tools that notify you when a folder's contents change); if it finds an image there, it will:

    • make a thumbnail (we were using ImageMagick's CLI for that)
    • save it somewhere else
    • update the database, set the image status as "thumbnailed OK" (or "failed", if it couldn't make one)

    When you need the thumbnail, check the thumbnails table - if the image is not "thumbnailed OK", show a placeholder, else get the correct thumbnail name and display this.

    That worked great - most thumbnails were created within a few seconds, without slowing down the user-facing scripts. Note that you'll need to start the background script somehow - in this case, there was a watchdog in cron, which restarted the thumbnailing script if it died.


    @yankee objects that some elements are uncommon:

    • it is not necessary for the thumbnailer process to run as a background script - if you can live with a minute of latency before you get the thumbnails, you could run it as a cron script, getting rid of the watchdog altogether.
    • ImageMagick was chosen over GD for specific performance reasons; the thumbnailer could use whatever method is available.

    Edit: I checked the site, and there is one more mechanism - this one is not necessary and adds a bit of load, but looks cool, especially if you don't expect full page loads very often (e.g. on AJAX-driven sites):

    • where a "not-failed-but-no-thumbnail" placeholder is output, the thumbnail is shown in an img with class="nothumb"
    • a JS function checks for images with this class
    • if any are found, it will periodically check if a thumbnail is available yet
    • the static placeholders are replaced with "loading" placeholders
    • if found, it will replace the placeholder with the thumbnail

    This loads the thumbnails as soon as they are ready, at the cost of some resources. For a continuous background process, it's not really needed; but if you want to ensure that users will see the thumbnails as they become available instead of on their next pageload, this is an useful addition.

    0 讨论(0)
提交回复
热议问题