Run PHP Task Asynchronously

后端 未结 15 1326
青春惊慌失措
青春惊慌失措 2020-11-22 15:15

I work on a somewhat large web application, and the backend is mostly in PHP. There are several places in the code where I need to complete some task, but I don\'t want to m

15条回答
  •  青春惊慌失措
    2020-11-22 15:40

    Another way to fork processes is via curl. You can set up your internal tasks as a webservice. For example:

    • http://domain/tasks/t1
    • http://domain/tasks/t2

    Then in your user accessed scripts make calls to the service:

    $service->addTask('t1', $data); // post data to URL via curl
    

    Your service can keep track of the queue of tasks with mysql or whatever you like the point is: it's all wrapped up within the service and your script is just consuming URLs. This frees you up to move the service to another machine/server if necessary (ie easily scalable).

    Adding http authorization or a custom authorization scheme (like Amazon's web services) lets you open up your tasks to be consumed by other people/services (if you want) and you could take it further and add a monitoring service on top to keep track of queue and task status.

    • http://domain/queue?task=t1
    • http://domain/queue?task=t2
    • http://domain/queue/t1/100931

    It does take a bit of set-up work but there are a lot of benefits.

提交回复
热议问题