Skip to content
This repository has been archived by the owner on Oct 1, 2023. It is now read-only.

Add advanced async helper classes

Compare
Choose a tag to compare
@fredemmott fredemmott released this 13 May 17:33

This release adds:

  • Async\Poll and Async\KeyedPoll: these classes allow you to iterate over a group of awaitables as each finishes
  • Async\Condition: a convenience wrapper around ConditionWaitHandle
  • Async\ConditionNode: a convenience wrapper for building linked lists around Async\Condition
  • Async\Semaphore: a semaphore implementation for async code, allowing limiting concurrency when the awaitables that need limiting are not fully known at the start of execution

This release supports HHVM 4.1 and above.

We strongly recommend avoiding Async\Poll and Async\KeyedPoll wherever possible: while they can be used to solve many problems more easily than other approaches, they can lead to much more inefficient code than alternative approaches, such as storing awaitables or building linked lists of awaitables.

In some cases there is not a more efficient approach, but Async\Poll can hide the performance impact, leading to over-use. In particular, assuming they complete before the end of a request, all awaitables passed to it will complete, even if not requested. For example, if you use Async\Poll to request the same data from memcached, mysql, and an HTTP request endopint, and memcached responds first, CPU and network resources will still be used for the MySQL and CURL requests until they complete, even if the result is unused - and this will not appear in function-level profilers.

Async\Poll should primarily be used when the results of all awaitables are required, but ordering is not.

The primary advantages of using Async\Poll over simply not awaiting awaitables are:

  • if iterated until completion, all awaitables will definitely complete
  • if an awaitable throws an exception but is not awaited, the exception will disappear