SPACE.LA
◀ music select
personal / library / archived

Timeout

A PHP implementation of JavaScript's setTimeout for asynchronous PHP: runs a closure in a background process (serialized with SuperClosure) so the rest of the script continues, written in 2012 for firing off mail and Twilio/Phaxio API callbacks without blocking.

BPM
167
GRADE
B
LEVEL
6
RELEASES
0
YEARS
2012–2013

setTimeout For PHP

PHP is synchronous, so when a customer placed an order, the page had to wait on a mail server, then Twilio, then whatever else before it could come back. In November 2012, at Crunchbutton, that was a problem, so I wrote Timeout.

It's a PHP version of JavaScript's setTimeout. You give it a closure and a delay, it runs it in a background process, and your script keeps going. The first example was a one-liner that sends mail a second later while the request finishes without it. The use cases were mail and third-party API callbacks like Twilio or Phaxio, both of which I was dealing with at the time.

Underneath it used SuperClosure, Jeremy Lindblom's library, which serialized closures well enough to pass objects across the process boundary. My second example built a User object with Name Devin, Location LA, and Food set to Sushi and Beer, then closed over it, with a warning to require the class definitions first or the decode would come back incomplete. I worked on it for about three months.