Fire a (multi) curl request and don't wait for the response (PHP)

给你一囗甜甜゛ 提交于 2019-12-24 09:48:09

问题


I want to fire a set of PHP script (through multi_curl) and not wait for a response from the caller script (200 OK):

I have an aggregator that runs searches on social media network APIs asking them for messages. When it gets a response it fires up another search for messages that were earlier (paging). This can take quite a long time and I don't want to make users wait for this to finish.

I would like to be able to fire up a set of PHP scripts from one main script, if you will, in the background (multi_curl?) that will each take care of grabbing all these messages, each script will have its network and keyword to look on and for. I want the main script to just call these other scripts and then not wait for them to finish and just print out 'requests fired, running in background' or something of the sort. It will return JSON, so it would have to properly close and give 200 OK.

How can I do this? So fire some multi_curls and not wait for them to finish?


回答1:


I'm not privy to your particular setup, but you might be able to create a single script that runs your PHP scripts in the background with a bash script like this:

nohup php /path/to/script.1.php &
nohup php /path/to/script.2.php &
nohup php /path/to/script.3.php &

The ampersand at the end tells it to run the script in the background. the "nohup" command tells it to ignore any hangup signal, ensuring the script will continue running even if the user logs out.




回答2:


Have a look at the PHP port of Ruby's delayed_job.

https://github.com/seatgeek/djjob



来源:https://stackoverflow.com/questions/6904505/fire-a-multi-curl-request-and-dont-wait-for-the-response-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!