How to monitor time remaining when running a php script looping?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have a script that reads the directory and make a list of files, including some date about each file. A simple script like millions of others. Depending on the number of files in the directory and the availability of the server (is a shared hosting server), this will result in a timeout error.



Instead of just increase time_limit (might not even to be allowed), I was thinking about to monitor the time remaining each step of the way, I might suspend the task (exit the loop) return the list processed so far, and the process can be resumed from that point with a new request.



This can be done in php? How? What is the alternative excluding time_limit?










share|improve this question























  • $end = time() + $limit; while(...) { ...; if( time() > $end ) { break; } } Where $limit is however long your timeout is expected to be, minus at least 1x whatever the longest possible time a loop iteration could take, minus time for post-loop code to complete. Ideally though you should ditch shared hosting and dive into public cloud so you could do something more productive like an asynchronous queue/worker system where you don't have to worry about individual request timeouts.

    – Sammitch
    Nov 16 '18 at 18:44













  • time will return seconds, not very accurate. Also I am worry about when the clock really start ticking. In my experience we might miss some precious time before to get the timestamp, some delay. Though we need something from the php core, if the server is counting time, the value must be somewhere, but I could not find it, don't know if actually exists a way to get it.

    – Gustavo
    Nov 16 '18 at 19:57






  • 2





    No, you cannot interrogate PHP to know how much time is remaining. This is a ridiculous problem to have, and you should try to not be in this situation at all in the first place.

    – Sammitch
    Nov 16 '18 at 20:07











  • I tried not do be in this ridiculous situation but I haven't got an answer yet, of course. This is a real problem of a real world, like listing a large number of files of a directory (and other challenges), you guys don't have an answer call my problem as "ridiculous situation"... sorry if this is embarrassing for you, will take my ridiculous questions elsewhere!

    – Gustavo
    Nov 17 '18 at 0:12






  • 1





    I gave you a perfectly workable suggestion, which you then rejected out-of-hand because seconds are "not very accurate". I even gave you a bonus suggestion of a better way to approach the general problem which doesn't land you in the Zone of Ridiculousness, and I'm just going to assume you're rejecting that out of hand as well.Have fun on your adventure.

    – Sammitch
    Nov 17 '18 at 0:52


















0















I have a script that reads the directory and make a list of files, including some date about each file. A simple script like millions of others. Depending on the number of files in the directory and the availability of the server (is a shared hosting server), this will result in a timeout error.



Instead of just increase time_limit (might not even to be allowed), I was thinking about to monitor the time remaining each step of the way, I might suspend the task (exit the loop) return the list processed so far, and the process can be resumed from that point with a new request.



This can be done in php? How? What is the alternative excluding time_limit?










share|improve this question























  • $end = time() + $limit; while(...) { ...; if( time() > $end ) { break; } } Where $limit is however long your timeout is expected to be, minus at least 1x whatever the longest possible time a loop iteration could take, minus time for post-loop code to complete. Ideally though you should ditch shared hosting and dive into public cloud so you could do something more productive like an asynchronous queue/worker system where you don't have to worry about individual request timeouts.

    – Sammitch
    Nov 16 '18 at 18:44













  • time will return seconds, not very accurate. Also I am worry about when the clock really start ticking. In my experience we might miss some precious time before to get the timestamp, some delay. Though we need something from the php core, if the server is counting time, the value must be somewhere, but I could not find it, don't know if actually exists a way to get it.

    – Gustavo
    Nov 16 '18 at 19:57






  • 2





    No, you cannot interrogate PHP to know how much time is remaining. This is a ridiculous problem to have, and you should try to not be in this situation at all in the first place.

    – Sammitch
    Nov 16 '18 at 20:07











  • I tried not do be in this ridiculous situation but I haven't got an answer yet, of course. This is a real problem of a real world, like listing a large number of files of a directory (and other challenges), you guys don't have an answer call my problem as "ridiculous situation"... sorry if this is embarrassing for you, will take my ridiculous questions elsewhere!

    – Gustavo
    Nov 17 '18 at 0:12






  • 1





    I gave you a perfectly workable suggestion, which you then rejected out-of-hand because seconds are "not very accurate". I even gave you a bonus suggestion of a better way to approach the general problem which doesn't land you in the Zone of Ridiculousness, and I'm just going to assume you're rejecting that out of hand as well.Have fun on your adventure.

    – Sammitch
    Nov 17 '18 at 0:52














0












0








0








I have a script that reads the directory and make a list of files, including some date about each file. A simple script like millions of others. Depending on the number of files in the directory and the availability of the server (is a shared hosting server), this will result in a timeout error.



Instead of just increase time_limit (might not even to be allowed), I was thinking about to monitor the time remaining each step of the way, I might suspend the task (exit the loop) return the list processed so far, and the process can be resumed from that point with a new request.



This can be done in php? How? What is the alternative excluding time_limit?










share|improve this question














I have a script that reads the directory and make a list of files, including some date about each file. A simple script like millions of others. Depending on the number of files in the directory and the availability of the server (is a shared hosting server), this will result in a timeout error.



Instead of just increase time_limit (might not even to be allowed), I was thinking about to monitor the time remaining each step of the way, I might suspend the task (exit the loop) return the list processed so far, and the process can be resumed from that point with a new request.



This can be done in php? How? What is the alternative excluding time_limit?







php time timeoutexception






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 18:37









GustavoGustavo

80531931




80531931













  • $end = time() + $limit; while(...) { ...; if( time() > $end ) { break; } } Where $limit is however long your timeout is expected to be, minus at least 1x whatever the longest possible time a loop iteration could take, minus time for post-loop code to complete. Ideally though you should ditch shared hosting and dive into public cloud so you could do something more productive like an asynchronous queue/worker system where you don't have to worry about individual request timeouts.

    – Sammitch
    Nov 16 '18 at 18:44













  • time will return seconds, not very accurate. Also I am worry about when the clock really start ticking. In my experience we might miss some precious time before to get the timestamp, some delay. Though we need something from the php core, if the server is counting time, the value must be somewhere, but I could not find it, don't know if actually exists a way to get it.

    – Gustavo
    Nov 16 '18 at 19:57






  • 2





    No, you cannot interrogate PHP to know how much time is remaining. This is a ridiculous problem to have, and you should try to not be in this situation at all in the first place.

    – Sammitch
    Nov 16 '18 at 20:07











  • I tried not do be in this ridiculous situation but I haven't got an answer yet, of course. This is a real problem of a real world, like listing a large number of files of a directory (and other challenges), you guys don't have an answer call my problem as "ridiculous situation"... sorry if this is embarrassing for you, will take my ridiculous questions elsewhere!

    – Gustavo
    Nov 17 '18 at 0:12






  • 1





    I gave you a perfectly workable suggestion, which you then rejected out-of-hand because seconds are "not very accurate". I even gave you a bonus suggestion of a better way to approach the general problem which doesn't land you in the Zone of Ridiculousness, and I'm just going to assume you're rejecting that out of hand as well.Have fun on your adventure.

    – Sammitch
    Nov 17 '18 at 0:52



















  • $end = time() + $limit; while(...) { ...; if( time() > $end ) { break; } } Where $limit is however long your timeout is expected to be, minus at least 1x whatever the longest possible time a loop iteration could take, minus time for post-loop code to complete. Ideally though you should ditch shared hosting and dive into public cloud so you could do something more productive like an asynchronous queue/worker system where you don't have to worry about individual request timeouts.

    – Sammitch
    Nov 16 '18 at 18:44













  • time will return seconds, not very accurate. Also I am worry about when the clock really start ticking. In my experience we might miss some precious time before to get the timestamp, some delay. Though we need something from the php core, if the server is counting time, the value must be somewhere, but I could not find it, don't know if actually exists a way to get it.

    – Gustavo
    Nov 16 '18 at 19:57






  • 2





    No, you cannot interrogate PHP to know how much time is remaining. This is a ridiculous problem to have, and you should try to not be in this situation at all in the first place.

    – Sammitch
    Nov 16 '18 at 20:07











  • I tried not do be in this ridiculous situation but I haven't got an answer yet, of course. This is a real problem of a real world, like listing a large number of files of a directory (and other challenges), you guys don't have an answer call my problem as "ridiculous situation"... sorry if this is embarrassing for you, will take my ridiculous questions elsewhere!

    – Gustavo
    Nov 17 '18 at 0:12






  • 1





    I gave you a perfectly workable suggestion, which you then rejected out-of-hand because seconds are "not very accurate". I even gave you a bonus suggestion of a better way to approach the general problem which doesn't land you in the Zone of Ridiculousness, and I'm just going to assume you're rejecting that out of hand as well.Have fun on your adventure.

    – Sammitch
    Nov 17 '18 at 0:52

















$end = time() + $limit; while(...) { ...; if( time() > $end ) { break; } } Where $limit is however long your timeout is expected to be, minus at least 1x whatever the longest possible time a loop iteration could take, minus time for post-loop code to complete. Ideally though you should ditch shared hosting and dive into public cloud so you could do something more productive like an asynchronous queue/worker system where you don't have to worry about individual request timeouts.

– Sammitch
Nov 16 '18 at 18:44







$end = time() + $limit; while(...) { ...; if( time() > $end ) { break; } } Where $limit is however long your timeout is expected to be, minus at least 1x whatever the longest possible time a loop iteration could take, minus time for post-loop code to complete. Ideally though you should ditch shared hosting and dive into public cloud so you could do something more productive like an asynchronous queue/worker system where you don't have to worry about individual request timeouts.

– Sammitch
Nov 16 '18 at 18:44















time will return seconds, not very accurate. Also I am worry about when the clock really start ticking. In my experience we might miss some precious time before to get the timestamp, some delay. Though we need something from the php core, if the server is counting time, the value must be somewhere, but I could not find it, don't know if actually exists a way to get it.

– Gustavo
Nov 16 '18 at 19:57





time will return seconds, not very accurate. Also I am worry about when the clock really start ticking. In my experience we might miss some precious time before to get the timestamp, some delay. Though we need something from the php core, if the server is counting time, the value must be somewhere, but I could not find it, don't know if actually exists a way to get it.

– Gustavo
Nov 16 '18 at 19:57




2




2





No, you cannot interrogate PHP to know how much time is remaining. This is a ridiculous problem to have, and you should try to not be in this situation at all in the first place.

– Sammitch
Nov 16 '18 at 20:07





No, you cannot interrogate PHP to know how much time is remaining. This is a ridiculous problem to have, and you should try to not be in this situation at all in the first place.

– Sammitch
Nov 16 '18 at 20:07













I tried not do be in this ridiculous situation but I haven't got an answer yet, of course. This is a real problem of a real world, like listing a large number of files of a directory (and other challenges), you guys don't have an answer call my problem as "ridiculous situation"... sorry if this is embarrassing for you, will take my ridiculous questions elsewhere!

– Gustavo
Nov 17 '18 at 0:12





I tried not do be in this ridiculous situation but I haven't got an answer yet, of course. This is a real problem of a real world, like listing a large number of files of a directory (and other challenges), you guys don't have an answer call my problem as "ridiculous situation"... sorry if this is embarrassing for you, will take my ridiculous questions elsewhere!

– Gustavo
Nov 17 '18 at 0:12




1




1





I gave you a perfectly workable suggestion, which you then rejected out-of-hand because seconds are "not very accurate". I even gave you a bonus suggestion of a better way to approach the general problem which doesn't land you in the Zone of Ridiculousness, and I'm just going to assume you're rejecting that out of hand as well.Have fun on your adventure.

– Sammitch
Nov 17 '18 at 0:52





I gave you a perfectly workable suggestion, which you then rejected out-of-hand because seconds are "not very accurate". I even gave you a bonus suggestion of a better way to approach the general problem which doesn't land you in the Zone of Ridiculousness, and I'm just going to assume you're rejecting that out of hand as well.Have fun on your adventure.

– Sammitch
Nov 17 '18 at 0:52












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343602%2fhow-to-monitor-time-remaining-when-running-a-php-script-looping%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343602%2fhow-to-monitor-time-remaining-when-running-a-php-script-looping%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine