Introducing asyncify

Photo by Nathan da Silva on Unsplash

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.

I write PHP and I know things — kind of.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Zen in the Art of Software Architecture

How I Started With My Online Android Certification Course For Free — Stage 1

AZ 900 — Prepare in a week

EASIEST Introduction To Non-Fungible Tokens(For Beginners!)

Postgres Series: Practical User/Role Commands Cheatsheet

Awesome hacks from TerribleHack 6

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Lochemem Bruno Michael

Lochemem Bruno Michael

I write PHP and I know things — kind of.

More from Medium

Receive and Parse a CSV file with NestJs — Part 1

Announcing Ditsmod 2.0 beta (with RealWorld example)

How to execute PHP cron job on the new Da-manager control panel(Cpanel)

Websocket with Cloudflare Tunnel Reversed Proxy to Self-hosted Ubuntu Server