# [PATCH] Tick functions (on C level) with state

- Thread: https://externals.io/message/85739
- Original archive: https://news-web.php.net/php.internals/85739
- Started by Andrey Hristov on 2015-04-07T13:43:42+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

---

## #85739 — Andrey Hristov — 2015-04-07T13:43:42+00:00

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

Hi,
I would like to hear comments about a small change to the low-level tick 
functionality (main/php_ticks.c) to add state to when calling a C tick 
function
The patch is alive at : http://pastebin.com/0zhVNxaY

Currently a C tick function gets as a parameter just the number of 
ticks, and can't preserve state between calls. On PHP level one can use 
closures to mitigate this, but in the C world we don't have this luxury.

Any comments are very welcome. If possible to get in PHP7.


Cheers,
Andrey

---

## #85740 — Dmitry Stogov — 2015-04-07T14:33:57+00:00

URL: https://externals.io/message/85739#85740 · In reply to: #85739

Hi Andrey,

I didn't get why do you need this extension and how it's going to be used.

Thanks. Dmitry.



>  Hi,
> I would like to hear comments about a small change to the low-level tick
> functionality (main/php_ticks.c) to add state to when calling a C tick
> function
> The patch is alive at : http://pastebin.com/0zhVNxaY
>
> Currently a C tick function gets as a parameter just the number of ticks,
> and can't preserve state between calls. On PHP level one can use closures
> to mitigate this, but in the C world we don't have this luxury.
>
> Any comments are very welcome. If possible to get in PHP7.
>
>
> Cheers,
> Andrey

---

## #85741 — Andrey Hristov — 2015-04-07T14:56:37+00:00

URL: https://externals.io/message/85739#85741 · In reply to: #85740

Hi Dmitry,

> Hi Andrey,
>
> I didn't get why do you need this extension and how it's going to be used.

I would like to implement  (I have a PoC for this too), asynchronous 
query handlers in mysqli@mysqlnd. Currently it is possible to have some 
asynchronisity like

$c = mysqli_query("....", MYSQLI_ASYNC);

// do some work
// poll with mysqli_poll

// synchronise with mysqli_reap_async_query

// fetch data

I would like to skip the //poll and in some cases the //synchronise part 
and have

$c = mysqli_query("....", MYSQLI_ASYNC,
    function () use ($c) {
       $res = mysqli_reap_async_query($c);
       // do something with the result
    }
);

The closure can also set some global flag, and somewhere in the "main" 
thread it one can synchronize on it. It can also use [class, method] 
callback syntax. For example one can subclass class mysqli, add some 
status variables for state, and a method to pass to `mysqli_query()`/ 
mysqli::query().

mysqlnd registers a small tick function, that does select, and if there 
is data to be read the closure is called. Another closure for handling 
an error should be added too (but I don't have it my PoC).

Hope this helps to enlighten my intentions.

Best,
Andrey


> Thanks. Dmitry.
>
> On Tue, Apr 7, 2015 at 4:43 PM, Andrey Hristov <php@hristov.com
> <mailto:php@hristov.com>> wrote:
>
>       Hi,
>     I would like to hear comments about a small change to the low-level
>     tick functionality (main/php_ticks.c) to add state to when calling a
>     C tick function
>     The patch is alive at : http://pastebin.com/0zhVNxaY
>
>     Currently a C tick function gets as a parameter just the number of
>     ticks, and can't preserve state between calls. On PHP level one can
>     use closures to mitigate this, but in the C world we don't have this
>     luxury.
>
>     Any comments are very welcome. If possible to get in PHP7.
>
>
>     Cheers,
>     Andrey

---

## #85742 — Dmitry Stogov — 2015-04-07T15:31:50+00:00

URL: https://externals.io/message/85739#85742 · In reply to: #85741

This is really interesting, but I think use of "ticks" for asynchronous
functions is bad approach.
A month or two ago I published a PoC for safe interrupt handling.

https://github.com/php/php-src/compare/master...dstogov:interrupt

Actually it's already partially committed to support timeout handling on
Windows.

This idea may be extended to implement cooperative-multitasking,
asynchronous callbacks, signal handlers, etc...
I didn't continue work on it yet, because it's too later for PHP-7.0 and I
have a lot of other PHP-7 related work.

If you like to implement `MYSQLI_ASYNC` PoC now - just include your ticks
extension there.
If we accept it, we will accept all together.

Thanks. Dmitry.




>  Hi Dmitry,
> 
>
> > […]
>
> I would like to implement  (I have a PoC for this too), asynchronous query
> handlers in mysqli@mysqlnd. Currently it is possible to have some
> asynchronisity like
>
> $c = mysqli_query("....", MYSQLI_ASYNC);
>
> // do some work
> // poll with mysqli_poll
>
> // synchronise with mysqli_reap_async_query
>
> // fetch data
>
> I would like to skip the //poll and in some cases the //synchronise part
> and have
>
> $c = mysqli_query("....", MYSQLI_ASYNC,
>    function () use ($c) {
>       $res = mysqli_reap_async_query($c);
>       // do something with the result
>    }
> );
>
> The closure can also set some global flag, and somewhere in the "main"
> thread it one can synchronize on it. It can also use [class, method]
> callback syntax. For example one can subclass class mysqli, add some status
> variables for state, and a method to pass to `mysqli_query()`/
> mysqli::query().
>
> mysqlnd registers a small tick function, that does select, and if there is
> data to be read the closure is called. Another closure for handling an
> error should be added too (but I don't have it my PoC).
>
> Hope this helps to enlighten my intentions.
>
> Best,
> Andrey
>
>
>  Thanks. Dmitry.
> > […]

---

## #88493 — Anatol Belski — 2015-09-25T08:17:32+00:00

URL: https://externals.io/message/85739#88493

FYI the patch http://pastebin.com/wVymMAdZ , forwarding as seems my mail filters put it into the internals folder while there was no internals in the "to" list.

Regards

Anatol

-----Original Message-----
From: Anatol Belski [mailto:anatol.php@belski.net] 
Sent: Thursday, September 24, 2015 6:49 PM
To: 'Andrey Hristov' <andrey.hristov@oracle.com>; 'Dmitry Stogov' <dmitry@zend.com>
Cc: 'Anatol Belski' <ab@php.net>; 'Nikita Popov' <nikita.ppv@gmail.com>
Subject: RE: [PHP-DEV] Re: [PATCH] Tick functions (on C level) with state

Andrey,

Thanks for the explanation. Please proceed the patch. The risks of it are quite low and the benefits from the new MySQL features would outweigh in any case :)

Regards

Anatol

> -----Original Message-----
> From: Andrey Hristov [mailto:andrey.hristov@oracle.com]
> Sent: Thursday, September 24, 2015 5:32 PM
> 
>   Hi Dmitry, Anatol,
> this is the only thing that I would like to go into 7.0 . The code 
> that will use the new ticks will be a mysqlnd plugin and will live in PECL.
> If the functionality is accepted by the community, then it will find 
> its way into mysqli/mysqlnd. This way mysqli/mysqlnd will not be destabilized in 7.0 .
> 
> Regarding the ticks, if no extension registers them, there is no 
> overhead. The PECL extension, which will overload mysqlnd semantics, 
> like mysqlnd-ms, mysqlnd-mc etc. do, will be declared experimental. 
> When using experimental features things can be slower. A warning will be put in the documentation.
> 
> All in all, this will help testing new features but in the common case 
> it won't hurt anyone who is not using ticks. The patch doesn't make 
> the performance of ticks worse, it allows flexibility :)
> 
> I am sure there is a better way to do async, however adding it is not 
> as simple as this patch.
> 
> 
> Best,
> Andrey
