# TameJS syntax for Async/Parallel execution in PHP

- Thread: https://externals.io/message/54395
- Original archive: https://news-web.php.net/php.internals/54395
- Started by Raymond Irving on 2011-08-05T01:54:25+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #54395 — Raymond Irving — 2011-08-05T01:54:25+00:00

URL: https://externals.io/message/54395#54395

Hello,

I came across this little library called TameJS (http://tamejs.org/) and
fell in love with the it's syntax. This had me thinking if it was possible
to add such features to a PHP CLI (or web app):

<?php

await { // wait until all calls within this block is
    mysql_query_async($sql, $args, defer($rs)); // call asynchronous query
    curl_post_async($url,$data, defer($response)); // make asynchronous http
request
}
$rows = $rs->fetchAll();
// do something here

?>

The "await" keyword marks a section of code that depends on externals
events, like network or disk activity, or a timer. An await block contains
one or more calls to defer.

I think the above syntax makes it easier to write asynchronous apps that can
take advantage of multiple threads or processors


What do you think?


__
Raymond

---

## #54400 — Jezz Goodwin — 2011-08-05T08:20:18+00:00

URL: https://externals.io/message/54395#54400 · In reply to: #54395

This looks similar to Twisted in Python (http://twistedmatrix.com/trac/) 
and NodeJS (http://nodejs.org/) which are both non-blocking event driven 
IO engines.

The actual event driven side of things could be written today in PHP. 
There's nothing stopping you from writing an event call stack that 
manages call backs.

The problem will occur when you try to do any IO in a asynchronous 
manner. As far as I know all of the PHP IO operations happen in sync 
(there is a pause in the script while waiting for the IO event to return.)

So, basically async versions of all the needed IO calls (MySQL, 
MemCache, FileIO etc) would need to be created which can handle a 
callback function. These could be done initially with PECL modules. (no 
reason to touch the core to get these working).

Apparently (according to Ryan Dahl of NodeJs) making async calls isn't 
as easy as it could be. Especially in MySQL as the base MySQL libraries 
in C are blocking (synchronous).

If you're interested in non-blocking IO, I recommend watching the videos 
on NodeJs.org




> Hello,
>
> I came across this little library called TameJS (http://tamejs.org/) and
> fell in love with the it's syntax. This had me thinking if it was possible
> to add such features to a PHP CLI (or web app):
>
> <?php
>
> await { // wait until all calls within this block is
>      mysql_query_async($sql, $args, defer($rs)); // call asynchronous query
>      curl_post_async($url,$data, defer($response)); // make asynchronous http
> request
> }
> $rows = $rs->fetchAll();
> // do something here
>
> ?>
>
> The "await" keyword marks a section of code that depends on externals
> events, like network or disk activity, or a timer. An await block contains
> one or more calls to defer.
>
> I think the above syntax makes it easier to write asynchronous apps that can
> take advantage of multiple threads or processors
>
>
> What do you think?
>
>
> __
> Raymond

---

## #54401 — Jezz Goodwin — 2011-08-05T09:47:41+00:00

URL: https://externals.io/message/54395#54401 · In reply to: #54400

Ah, I've just done a bit of reading in to the PHP MySQL native driver.

It is already possible to do async mysql calls. The documentation in 
mysqli::query (http://www.php.net/manual/en/mysqli.query.php) references 
MYSQL_ASYNC.

So, if you're just using Mysql, I think there is enough functionality in 
PHP today to make the kind of program you're after.



> This looks similar to Twisted in Python 
> (http://twistedmatrix.com/trac/) and NodeJS (http://nodejs.org/) which 
> are both non-blocking event driven IO engines.
>
> The actual event driven side of things could be written today in PHP. 
> There's nothing stopping you from writing an event call stack that 
> manages call backs.
>
> The problem will occur when you try to do any IO in a asynchronous 
> manner. As far as I know all of the PHP IO operations happen in sync 
> (there is a pause in the script while waiting for the IO event to 
> return.)
>
> So, basically async versions of all the needed IO calls (MySQL, 
> MemCache, FileIO etc) would need to be created which can handle a 
> callback function. These could be done initially with PECL modules. 
> (no reason to touch the core to get these working).
>
> Apparently (according to Ryan Dahl of NodeJs) making async calls isn't 
> as easy as it could be. Especially in MySQL as the base MySQL 
> libraries in C are blocking (synchronous).
>
> If you're interested in non-blocking IO, I recommend watching the 
> videos on NodeJs.org
>
>
>
> 
> > […]

---

## #54716 — Jonathan Bond-Caron — 2011-08-19T11:12:39+00:00

URL: https://externals.io/message/54395#54716 · In reply to: #54395

> Hello,
> 
> I came across this little library called TameJS (http://tamejs.org/) 
> and fell in love with the it's syntax. This had me thinking if it was 
> possible to add such features to a PHP CLI (or web app):
> 

Thanks for sharing, never heard of tame. Love the await{} block

await {
  mysql_query_async($sql, $args, defer($rs));
  curl_post_async($url,$data, defer($response));
}
$rows = $rs->fetchAll();

> What do you think?

I think it would be really interesting r&d but likely a large undertaking to
get it to work within php

Also async processing makes a lot of sense of in the client since there's a
lot of idle processing power in the pc (dual core cpu etc..)

If many php processes are executing the same async code (server under load),
I'm not sure you'd get a net benefit.

My opinion, server-side caching is faster and more maintainable.

---

## #54726 — Ulf Wendel — 2011-08-19T12:44:04+00:00

URL: https://externals.io/message/54395#54726 · In reply to: #54716

Am 19.08.2011 13:12, schrieb Jonathan Bond-Caron:
> 
> > […]
>
> Thanks for sharing, never heard of tame. Love the await{} block
>
> await {
>    mysql_query_async($sql, $args, defer($rs));
>    curl_post_async($url,$data, defer($response));
> }
> $rows = $rs->fetchAll();

http://de.php.net/manual/en/mysqli.query.php

[...]
With *MYSQLI_ASYNC (available with mysqlnd)*, it is possible to perform 
query asynchronously. `mysqli_poll()` is then used to get results from 
such queries.
[...]

PG should have similar functionality.


Ulf
