我有一個簡單的函數1,它向http api執行http請求並返回$ result。 然後我有另一個function2,如果$ result isset,應該使用$ result做一些計算,然後返回$ finalresult。 。如何在下一個函數啓動之前等待變量返回
我的問題是,調用谷歌api需要幾秒鐘,並由時間$ result返回的function1,function2已經返回$ finalresult沒有考慮$結果。
我期望做的是讓function1完全運行,並在function2開始前返回$ result。
最好我正在尋找一種不是簡單地使用「sleep()」的解決方案,因爲此函數不能保證實際返回$ result。 (除非有某種方式來循環睡眠(1)至$回報isset,或類似的東西)
示例代碼爲視覺加侖和球員
function1_geocode($address); // this function makes a http request to google and returns $result
function2_proximitysearch(){
if (isset($result)){
//inevitably by the time the script gets there, $result hasn't been returned yet therefore none of the "some stuff" code is executed.
//some stuff
}
else {
//some other stuff
}
}
有時它是一種祝福,有時它是一種詛咒...... – pxl 2010-08-04 22:25:36
這是一種祝福。相信我,你不會希望被迫隨時進行併發編程。 – Mchl 2010-08-04 22:29:07