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?

11
  • 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. Commented Dec 12, 2012 at 18:27
  • 2
    php does support threading, but i don't think you need that for your particular problem Commented Dec 12, 2012 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. Commented Dec 12, 2012 at 19:11
  • 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. Commented Dec 12, 2012 at 19:17
  • 1
    Basically closing this. There is NO ADVANTAGE to using the tick function. Any delay in the tick function will delay code execution everywhere. Commented Dec 12, 2012 at 19:27

1 Answer 1

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.

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Am not sure if this download links leads to the latest versions
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
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 (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 -> stackoverflow.com/a/15501449/3470632

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.