Introducing asyncify

Lochemem Bruno Michael
2 min readAug 5, 2021

Combining synchronous PHP code — which accounts for the lion’s share of the language’s API — with asynchronous language routines is a tricky proposition. The two suites of artifacts are antipodes of each other. Whereas the former makes I/O less arduous by running in an event-driven environment, the latter conditions one to wait between successive, sequential executions. Considering asynchrony is garnering more mainstream appeal in PHP, it is prudent to muse on how best to execute blocking code in event-driven environments — à la-Node.JS. The result of my musings on the topic — asyncify — is a solution to the aforestated problem.

To install asyncifyvia Composer, type the following in a console of your choosing.

composer require chemem/asyncify

asyncify works solely in event-driven environments provisioned via ReactPHP and has a straightforward API. It utilizes a child-process API — also written in React and similar in principle to Node.JS’s child-process module — that leverages CPU resources to execute Operating System functions. asyncify subsumes the results of its computations in promises which are composable — and usable in non-blocking environments.

The snippet above demonstrates an asynchronous file_get_contents function call with ReactPHP’s new default loop. asyncify’s API is such that it offers a more FP-aligned, currying approach (for those like me who love shuttling functions around) — as well as a more traditional OOP avenue.

If you are considering working with event-driven systems in PHP in any capacity — as a casual hobbyist or more engaged, ardent apologist — consider giving asyncify a shot.

--

--