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. https://chemem.site

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

Recommended from Medium

ORICO External Mini Portable SSD USB 940MB/s 1TB 128GB 256GB 512GB Type — C Solid State Drive

ORICO External Mini Portable SSD USB 940MB/s 1TB 128GB 256GB 512GB Type - C Solid State Drive

Pull To Refresh in SwiftUI

Lessons learned building serverless data pipelines

TTY::Prompt 101

Make sure your web application is production ready

An identifier, by any other name, …

Salesforce Business Analyst Summit : Agile Webinar RECAP

Enabling science with the AWS CDK

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. https://chemem.site

More from Medium

Web Stack Weekly — Issue #7

Building Real-time Apps using WebSockets

Configure GRPC in macOS

I Started Programming All Over Again…