$30 off During Our Annual Pro Sale. View Details »

Swoole 4: Introducing the New Coroutine Design Pattern in PHP

Swoole
March 12, 2019

Swoole 4: Introducing the New Coroutine Design Pattern in PHP

Swoole

March 12, 2019
Tweet

More Decks by Swoole

Other Decks in Technology

Transcript

  1. 4:
    Introducing the New Coroutine Design Pattern in PHP
    Demin Yin

    View Slide

  2. About Me
    • Staff Engineer and Technical Lead on Glu Mobile’s Design Home team
    • Playing with PHP since 2000.
    • Worked for a diverse group of companies in China and US, including eBay, Visa and Glu
    • Focus on building high performance, secure web services
    • Born in China, worked in U.S., live in Canada
    • On twitter: @deminy
    • On Github: https://github.com/deminy
    2

    View Slide

  3. Agenda
    • What Is Swoole?
    • Benchmark Results
    • Swoole 4: Asynchronous Programming with Coroutines
    • Top Swoole Use Cases
    • History of Swoole, and What's New in Swoole 4
    • The Future of Swoole
    • Q & A
    3

    View Slide

  4. Agenda
    • What Is Swoole?
    • Benchmark Results
    • Swoole 4: Asynchronous Programming with Coroutines
    • Top Swoole Use Cases
    • History of Swoole, and What's New in Swoole 4
    • The Future of Swoole
    • Q & A
    4

    View Slide

  5. What Is Swoole: How Does Swoole Work?
    5
    under PHP-FPM under Swoole

    View Slide

  6. 6
    • Event-driven; asynchronous I/O.
    • Coroutines, channels, defer, and more.
    • Connection pool.
    • Build high performance, concurrent HTTP, WebSocket, TCP, UDP, Unix socket services.
    What Is Swoole:
    Enrich PHP with Some Amazing Features

    View Slide

  7. Agenda
    7
    • What Is Swoole?
    • Benchmark Results
    • Swoole 4: Asynchronous Programming with Coroutines
    • Top Swoole Use Cases
    • History of Swoole, and What's New in Swoole 4
    • The Future of Swoole
    • Q & A

    View Slide

  8. Benchmark Results: Single Query
    8
    Source: the TechEmpower Framework Benchmarks project
    best database-access responses per second, single query

    View Slide

  9. Benchmark Results: Multiple Queries
    9
    Source: the TechEmpower Framework Benchmarks project
    responses per second at 20 queries per request, 256 requests concurrently

    View Slide

  10. Benchmark Results: Fortunes
    10
    Source: the TechEmpower Framework Benchmarks project
    best fortunes responses per second

    View Slide

  11. Benchmark Results: Why Faster?
    11
    Why is Swoole faster?
    • Connection pool.
    • Extra operations when using PDO:
    • Message sent to MySQL server to deallocate the prepared statement when destroying a
    PDOStatement object.
    • Message sent to MySQL server to close the MySQL connection when destroying a PDO object.
    • ……

    View Slide

  12. Agenda
    12
    • What Is Swoole?
    • Benchmark Results
    • Swoole 4: Asynchronous Programming with Coroutines
    • Top Swoole Use Cases
    • History of Swoole, and What's New in Swoole 4
    • The Future of Swoole
    • Q & A

    View Slide

  13. Coroutine in Swoole 4: an example
    13
    under PHP under Swoole 4

    View Slide

  14. Coroutine in Swoole 4: an example (cont.)
    14
    under PHP under Swoole 4

    View Slide

  15. output: 123456
    Coroutine in Swoole 4: an example (cont.)
    15

    View Slide

  16. Processes, Threads, and Coroutines
    16
    Multiprocessing Multithreading Coroutine
    Creation fork() pthread_create() go()
    Scheduling expensive cheap extremely cheap
    Concurrency hundreds thousands hundreds of thousands
    Programming difficult very difficult easy

    View Slide

  17. Create a Server Socket in PHP
    17
    under PHP

    View Slide

  18. Create a Server Socket in Swoole 4
    18
    under Swoole 4

    View Slide

  19. Coroutine: What’s the benefits?
    19
    • A coroutine can be paused when IO happens, and resumed after IO finishes.
    • PHP code still executed in sequential manner.
    • No new processes or threads created. Low costs on “scheduling”.
    • No locks needed when updating shared resources from different coroutines.
    • High concurrency.
    • Easier to use, especially when comparing to asynchronous callbacks.

    View Slide

  20. Coroutines in For Loop
    20

    View Slide

  21. Nested Coroutines
    21

    View Slide

  22. Nested Coroutines (cont.)
    22

    View Slide

  23. Nested Coroutines (cont.)
    23
    output: 127983465 output: 123456789

    View Slide

  24. Blocking I/O in PHP
    24
    • PHP extension MySQLi, PDO: \Swoole\Coroutine\MySQL
    • PHP extension Redis: \Swoole\Coroutine\Redis
    • sleep(), usleep() : …
    • sockets and streams: …
    • file_get_contents(), fread(), fwrite() : …
    • ……
    • PHP extension SOAP.
    • PHP extension MongoDB.

    View Slide

  25. Swoole 4: Channels
    25

    View Slide

  26. Swoole 4: Channels (cont.)
    26
    SplQueue Channels
    push()
    SplQueue>push()
    Always successful until
    memory exhausted.
    chan::push()
    Blocks if running out of
    allocated space.
    pop()
    SplQueue>pop()
    Throws an exception
    when the queue is empty.
    chan::pop()
    Blocks if no data available.

    View Slide

  27. Swoole 4: Channels (cont.)
    27

    View Slide

  28. Swoole 4: defer
    28

    View Slide

  29. Swoole 4: defer (cont.)
    29
    Golang (pseudocode) PHP (pseudocode)

    View Slide

  30. Agenda
    30
    • What Is Swoole?
    • Benchmark Results
    • Swoole 4: Asynchronous Programming with Coroutines
    • Top Swoole Use Cases
    • History of Swoole, and What's New in Swoole 4
    • The Future of Swoole
    • Q & A

    View Slide

  31. Top Swoole Use Cases
    31
    • Crawlers.
    • HTTP/RPC.
    • IM, online chat room.
    • Internet of things.
    • Gaming servers.

    View Slide

  32. Agenda
    32
    • What Is Swoole?
    • Benchmark Results
    • Swoole 4: Asynchronous Programming with Coroutines
    • Top Swoole Use Cases
    • History of Swoole, and What's New in Swoole 4
    • The Future of Swoole
    • Q & A

    View Slide

  33. History of Swoole
    33
    • 2012-07: First Git commit.
    • 2013-04: Swoole 1 released.
    • 2016-12: Swoole 2 released.
    • 2018-06: Swoole 4 released.

    View Slide

  34. The Future of Swoole (cont.)
    34
    A dedicated development team for the Swoole ecosystem, with more supports to the open
    source community:
    • SDLC process improvements.
    • Improved documentation.
    • Lots of refactors.
    • More components like ZooKeeper, Kafka, Hadoop, HBase, etcd, grpc, AMQP, MQTT, etc.

    View Slide

  35. Swoole 4.1
    35
    • 4.1.0: Added stream hook: support asynchronous IO on Redis, PDO, MySQLi.
    • 4.1.2: Added socket hook: allow building coroutine versions of PHP extensions for
    MongoDB, ZooKeeper and more.

    View Slide

  36. Swoole 4.1: Stream Hook
    36

    View Slide

  37. Swoole 4.1: Stream Hook (cont.)
    37

    View Slide

  38. Swoole 4.1: Stream Hook (cont.)
    38

    View Slide

  39. Agenda
    39
    • What Is Swoole?
    • Benchmark Results
    • Swoole 4: Asynchronous Programming with Coroutines
    • Top Swoole Use Cases
    • History of Swoole, and What's New in Swoole 4
    • The Future of Swoole
    • Q & A

    View Slide

  40. The Future of Swoole
    40
    • Latest stable release: 4.3.0
    • Swoole 5.0
    • Coroutines only.
    • Clean up legacy code.

    View Slide

  41. The Future of Swoole (cont.)
    41
    lots of changes in Swoole since 2018
    Source: https://github.com/swoole/swoole-src/graphs/contributors

    View Slide

  42. Resources
    42
    • Sample code of this talk: https://github.com/deminy/swoole-by-examples
    • Swoole: https://github.com/swoole/swoole-src
    • Swoole frameworks:
    • https://github.com/zendframework/zend-expressive-swoole
    • https://github.com/easy-swoole/easyswoole
    • https://github.com/swoft-cloud/swoft/
    • https://github.com/swooletw/awesome-swoole

    View Slide

  43. 43
    QUESTIONS?

    View Slide

  44. 44
    Thanks
    Demin Yin
    Email: [email protected]
    Twitter: @deminy
    GitHub: https://github.com/deminy

    View Slide