PHP后台进程的逻辑解释

I'm a moderate to good PHP programer and have experience with terminal/shell scripts but what I'm trying to wrap my head around is the logics behind background processes and is most certainly not Cron or Cron Jobs but a continual flow of data.

I recently talked to someone who made a little web app that worked with the twitter streaming API and Phirehose to gather tweets and save them to a DB. Now sounds simple but all this happens in the background as a process. What I'm ineptly used to is:

call process -> process finishes -> handle data from process.

What is so different about this is that it happens all the time non stop. I remember there was also so talk of socket connection as well.

So my questions are:

  1. When executing a background process, is it a continual loop of specific function? That's all that I can logically conclude, or does it some how "stay open" and happen?
  2. What does a socket connection do in this equation?
  3. Is the any form of latency inherit from running this type of process?

I know this is not a "code specific" type of question but I can't find much information regarding this type of question.

1. When executing a background process, is it a continual loop of specific function? That's all that I can logically conclude, or does it some how "stay open" and happen?

No, there is not requirement of such a continual loop. A background process can just be invoked, run and finish as well. It than does not run any longer like any other process as well. Maybe not useful for a background process, but possible.

2. What does a socket connection do in this equation?

Sockets are sometimes used to allow communication between different processes, also worded IPC - Inter Process Communication.

3. Is the any form of latency inherit from running this type of process?

Yes, every form of indirection comes with a price. Additionally, if you run multiple processes in parallel, there is also some overhead for the computer system to manage these multiple processes (which it does anyway nowadays, but just saying, if there were only one process, there would be nothing to manage).

With PHP, it's most likely that a cronjob is scheduled to execute the scripts once every hour or so. The script doesn't run continuously.

PHP has many ways of connecting to resources, most of these use sockets. If you do file_get_contents() to connect to a webserver, you're using sockets as well, you might just not notice it.

If you want to take a tutorial on background processes: http://thedjbway.b0llix.net/daemontools/blabbyd.html - really useful. Daemontools makes it very easy to maintain backgound processes (daemons).