简体   繁体   English

多次调用函数而无需等待它完成

[英]Call a function multiple times without waiting for it to complete

I have a cron job that calls a script that iterates through some items and submits them as posts to Facebook Graph API every minute. 我有一个cron作业,它调用一个脚本来迭代一些项目,并每分钟将它们作为帖子提交给Facebook Graph API。 The issue is, each call takes a few seconds. 问题是,每次通话都需要几秒钟。 If there is more than 10 posts to be sent to the API in a given minute, the script runs longer than a minute and then starts causing issues when the script starts running again at the next minute. 如果在给定的分钟内有超过10个帖子要发送到API,则脚本运行时间超过一分钟,然后在下一分钟脚本再次开始运行时开始引发问题。

The general process is like this: 1. Each facebook profile posts every hour 2. Each of these profiles have a 'posting minute', which is the minute of the hour that they are posted at 3. A cron job runs every minute to see which profiles should be posted do, any given minute, and then posts to them 一般过程是这样的:1。每个facebook个人资料每小时发布一次2.这些个人资料中的每一个都有一个“发布分钟”,这是他们在3点发布的小时。每分钟都会运行一个cron作业应该发布哪些配置文件,任何给定的分钟,然后发布给他们

My question: Is it possible to continue the script immediately after calling the $facebook->api(...) method below, rather than waiting for it to complete before continuing? 我的问题:是否可以在调用下面的$ facebook-> api(...)方法后立即继续脚本,而不是在继续之前等待它完成? So that it can ensure to post to all profiles within the given minute, rather than potentially risk having too many profiles to post to and the script exceeding 60 seconds. 因此,它可以确保在给定的分钟内发布到所有配置文件,而不是潜在的风险,有太多的配置文件发布到脚本超过60秒。

$profilesThatNeedToBePostedTo = getProfilesToPostTo(date(i));
foreach($profilesThatNeedToBePostedTo as $profile)
{
    $facebook->api($endPoint, 'POST', $postData); // $postData and $endPoint omitted for brevity
}

what you are asking if in PHP you can do Asynchronous calls. 你问的是,如果在PHP中你可以做异步调用。 There are some solutions here: Asynchronous PHP calls 这里有一些解决方案: 异步PHP调用

Just to point there are not the most reliable options. 只是指出没有最可靠的选择。

Is not limiting number of post to 9 each time reduce the times you have exceeded the 60 seconds. 不限制帖子的数量每次减少到你已超过60秒的次数。 But what happens if the Facebook Graph is slow? 但是如果Facebook Graph很慢会发生什么? what do you do? 你是做什么?

Or run you code for 60 seconds and once the 60 seconds time is reached close the function. 或者运行代码60秒,一旦达到60秒的时间,关闭功能。 And let it run again the next time. 然后让它再次运行。

I am not sure if I understand your question hundred percent clearly. 我不确定我是否百分百理解你的问题。 You meant that you want to run the cronjob every minute. 你的意思是你想每分钟运行一次cronjob。 However, it takes longer than 1 minute to call facebook API. 但是,调用facebook API需要超过1分钟。 Do you mean that foreach loop may delay your cronjob? 你的意思是foreach循环可能会延迟你的cronjob吗? If so, I wonder if you did a right cronjob script, better to show your cronjob script. 如果是这样,我想知道你是否做了一个正确的cronjob脚本,更好地展示你的cronjob脚本。 Because I think php script should not affect your sh script(cronjob) 因为我认为php脚本不应该影响你的sh脚本(cronjob)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM