SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    <?php while(!sleep()){code();} G.Schuster's Avatar
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cancel a function call after a given time?

    Hi guys and girls,

    I'd liek to know if there's any way to cancel a function call after a given period.
    I've the case that I call a method through call_user_func_array() that loads some data with SOAP but I want to controll "externally" that this function runs for a max of e.g. 10 seconds and cancel it after that regardless of the SOAP result if it didn't return in time.
    Is there any way to to that?

    TIA!

  2. #2
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Multi-threading + PHP =

    A work around might be to instead write your function call to a temp file, and then use it as a system call.

    This would allow you to dump it to the background and then after 10 seconds you would have the option of trashing the process.
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not sure you did try with set_time_limit()
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The ideal solution would to handle timeouts from within the function.

  5. #5
    <?php while(!sleep()){code();} G.Schuster's Avatar
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bhanson View Post
    A work around might be to instead write your function call to a temp file, and then use it as a system call.

    This would allow you to dump it to the background and then after 10 seconds you would have the option of trashing the process.
    No chance as it's an API call.

    I am not sure you did try with set_time_limit()
    Doesn't work for single functions, this would kill the whole script and is disabled on most servers.

    The ideal solution would to handle timeouts from within the function.
    Sure, I know that, but I don't have control over all API functions so I'd like to control it fron the outside.

    Seems as there's no solution....hm...ok, than that's life

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Launching another script as a background process like suggested by bhanson is the easiest solution. You just need to wrap the api call so you can temporarily store the result either in a file or database. You could also use pipes to communicate between these processes if you don't want to temporarily store the result on the filesystem(proc_open()). In order for the parent script to remain in control, you would need to either use stream_set_timeout() on the pipe you're reading from, or stream_set_blocking() to set the stream to non blocking mode. This is so the script doesnt hang waiting for a response when you try to read the pipe, instead it returns immediately even if you told it to read 1000bytes and it could only read 57 bytes at this instant. You usually combine this with a while loop which continually checks the elapsed time and just keeps trying to read over and over until it teimsout, or it gets what it wants or EOF is reached.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •