47

What I'm looking to do is load in some data from a database or from memcache asynchronously.

I know, I know. "PHP doesn't support threading".

But what about register_tick_function? Isn't that indeed threading?

Has anyone done any kind of predictive preload or really any background processing utilizing register_tick_function?

Beachhouse
  • 4,972
  • 3
  • 25
  • 39
  • 2
    No, everything _halts_ on a tick function. `gearman` is a nice way to offload some work to another process. Or a bit of forking can be done it you're not using php in a webserver but from the command line. – Wrikken Dec 12 '12 at 18:27
  • 2
    php does support threading, but i don't think you need that for your particular problem – Ibu Dec 12 '12 at 18:27
  • @Wrikken, I understand that everything else halts on tick. But think of threading on a single processor, single threaded machine. That's how they implement threading. – Beachhouse Dec 12 '12 at 19:11
  • @Ibu, it does? Native threading? Do you have any more information on this? – Beachhouse Dec 12 '12 at 19:13
  • @Wrikken, thanks for the note on gearman. Very interesting, I'm not sure it'll work here because I need to load data into the memory available to the user that's run the page. But very interesting for other purposes. – Beachhouse Dec 12 '12 at 19:16
  • 1
    No, PHP does not support threading. Forking, yes, native threading, no. I assume Ibu confuses it with something else... And yeah, pseudo threading like that can be attempted that way with ticks, but does not scale well at all of course. – Wrikken Dec 12 '12 at 19:17
  • doesn't scale well in terms of running much code that way? Would you say that a database query or a memcache call would be too much? – Beachhouse Dec 12 '12 at 19:19
  • ... and for gearman, if you need _more_ then function arguments can handle, shared memory and/or memcached can be your friends (although memcached is designed to drop data, so keep in mind that should not cause fatal errors...). – Wrikken Dec 12 '12 at 19:19
  • Does not scale well = does not use the available hardware to it's fullest as I see it. – Wrikken Dec 12 '12 at 19:20
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21026/discussion-between-beachhouse-and-wrikken) – Beachhouse Dec 12 '12 at 19:25
  • 1
    Basically closing this. There is NO ADVANTAGE to using the tick function. Any delay in the tick function will delay code execution everywhere. – Beachhouse Dec 12 '12 at 19:27

1 Answers1

62

http://php.net/pthreads

http://docs.php.net/Thread

PHP certainly can support threading. Loading data from a SQL/NoSQL database in parallel is definitely a possibility. See the PHP manual, examples found in github and pecl packages, and a little bit more info on http://pthreads.org

Please note, the documentation did state that this is part of the core, this is ( my ) human error. You must install pthreads with a thread-safe version of php, it can be installed from pecl ( windows incuded, dlls @ http://windows.php.net/downloads/pecl/releases/pthreads ). Apologies.

Joe Watkins
  • 17,032
  • 5
  • 41
  • 62
  • 1
    Am not sure if this download links leads to the latest versions – Baba May 19 '13 at 18:39
  • I know this is old but I think it's worth noting that pthreads at this time (3.1.6) only supports php 7.2+ so you'll need to build php with zts enabled form a version of php greater than or equal to 7.2 – hot_barbara Feb 26 '18 at 01:38
  • Apparently both of those URLs php.net/pthreads and docs.php.net/Thread say that they (pthreads) are no longer maintained and to use parallel instead (https://www.php.net/manual/en/book.parallel.php), except the comments on the bottom of this answer make it sound like parallel is also a dead end -> https://stackoverflow.com/a/15501449/3470632 – Kevin Wheeler Sep 13 '22 at 04:02