This commit is contained in:
Daniil Gentili 2019-05-04 14:08:40 +02:00
parent 9439031729
commit 55b64aaf62
2 changed files with 16 additions and 0 deletions

View File

@ -16,12 +16,14 @@ Powered by [amphp](https://amphp.org), MadelineProto wraps the AMPHP APIs to pro
* [Async in callback handler](#async-in-callback-handler)
* [Wrapped async](#wrapped-async)
* [Ignored async](#ignored-async)
* [Blocking async](#blocking-async)
* [MadelineProto and AMPHP async APIs](#madelineproto-and-amphp-async-apis)
## Usage
What exactly __is__ **async**, you may ask, and how is it better than **threading** or **multiprocessing**?
Async is a relatively new programming pattern that allows you to easily write **non-blocking** code **as if you were using standard** blocking functions, all without the need for complex message exchange systems and synchronization handling for threaded programs, that only add overhead and complexity to your programs, making everything slower and error-prone.
That's very cool and all, you might think, but how exactly does this __async__ stuff work? Well, as it turns out, it's very easy.
Instead of writing code like this:
@ -40,6 +42,8 @@ It's really **that** easy, you just have to add a `yield` before calling Madelin
The `yield` will automatically **suspend** the execution of the function, letting the program do other stuff while the file is being downloaded.
Once the file is downloaded, execution is automatically **resumed** from that exact point in the function.
This means that you can handle multiple updates, download/upload multiple files all together in one process, as if you were writing normal synchronous code + making everything a lot faster.
## Loading the latest version of MadelineProto
In order to use the `yield` operator in MadelineProto, you have to load the **latest version** of MadelineProto from the **master** branch (alpha) by loading it through composer (`dev-master`) or with madeline.php:
@ -154,6 +158,15 @@ You can use the async version of MadelineProto functions **without** yield if yo
This is allowed, but the order of the function calls will not be guaranteed: you can use [call queues](https://docs.madelineproto.xyz/docs/USING_METHODS.html#queues) if you want to make sure the order of the calls remains the same.
### Blocking async
```php
$result = blocking_function();
```
Sometimes, you have to call non-async functions in your code: that is allowed in async MadelineProto, you just have to call your functions normally without `yield`.
However, you shouldn't do (or need to do) this, because this renders async completely useless.
AMPHP already provides async versions of curl, `file_get_contents`, MySQL, redis, postgres, and many more native PHP functions:
## MadelineProto and AMPHP async APIs
MadelineProto and AMPHP both provide a lot of async functions: all of MadelineProto's functions are async, for example; and AMPHP provides [multiple packages](https://amphp.org/packages) to work asynchronously with HTTP requests, websockets, databases (MySQL, redis, postgres, DNS, sockets and [much more](https://github.com/amphp/)!

View File

@ -15,6 +15,8 @@ This library can be used to easily interact with Telegram **without** the bot AP
It can login with a phone number (MTProto API), or with a bot token (MTProto API, **no bot API involved!**).
[It is now fully async](https://docs.madelineproto.xyz/docs/ASYNC.html)!
## Getting started
```php
@ -62,6 +64,7 @@ Tip: if you receive an error (or nothing), [send us](https://t.me/pwrtelegramgro
* [Async in callback handler](https://docs.madelineproto.xyz/docs/ASYNC.html#async-in-callback-handler)
* [Wrapped async](https://docs.madelineproto.xyz/docs/ASYNC.html#wrapped-async)
* [Ignored async](https://docs.madelineproto.xyz/docs/ASYNC.html#ignored-async)
* [Blocking async](https://docs.madelineproto.xyz/docs/ASYNC.html#blocking-async)
* [MadelineProto and AMPHP async APIs](https://docs.madelineproto.xyz/docs/ASYNC.html#madelineproto-and-amphp-async-apis)
* [Creating a client](https://docs.madelineproto.xyz/docs/CREATING_A_CLIENT.html)
* [Logging in](https://docs.madelineproto.xyz/docs/LOGIN.html)