Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Coroutine Based Concurrency with PHP and Swoole

70989edbe96c069f20f3112bc0eb9eb7?s=47 Bruce Dou
February 20, 2020

Coroutine Based Concurrency with PHP and Swoole

PHP Swoole has gained traction during the past several years. More and more people are interested with started to try PHP Swoole and use coroutine in PHP to build high performance, large scale web services, online game, micro-services.

70989edbe96c069f20f3112bc0eb9eb7?s=128

Bruce Dou

February 20, 2020
Tweet

More Decks by Bruce Dou

Other Decks in Programming

Transcript

  1. Coroutine Based Concurrency with PHP and Swoole Bruce Dou |

    @doubaokun bruce@transfon.com 20th Feb 2020 PHPUK2020
  2. About me • Founder at Transfon • Twitter: @doubaokun •

    Email: bruce@transfon.com • Linkedin: linkedin.com/in/brucedou • Github: github.com/doubaokun Twitter: @doubaokun
  3. Agenda • Rethinking about software development • Concurrency, I/O, Scheduling

    and OS • Coroutine: Cooperative multi-tasking • Swoole Coroutine Internal • Swoole • Coroutine and Swoole code samples • Demo Twitter: @doubaokun
  4. Rethinking about software development Twitter: @doubaokun

  5. Twitter: @doubaokun Mapping real world problems onto machine

  6. Execution Unit Twitter: @doubaokun

  7. I/O and Task Processing Twitter: @doubaokun

  8. Twitter: @doubaokun

  9. Twitter: @doubaokun Mapping real world problems onto machine with several

    levels abstraction
  10. Twitter: @doubaokun

  11. Twitter: @doubaokun Mapping real world problems onto machine with several

    levels abstraction with real world limitations in mind
  12. Twitter: @doubaokun 1. 1 CPU cycle 0.3 ns 2. Main

    memory access 120 ns 3. Goroutine 170 ns 4. Swoole Coroutine 190 ns 5. Linux OS context switch 1,000 ns 6. Linux thread launch 5,000 ns 7. Linux process launch 20,000 ns 8. Per-thread stack is 8MB (ulimit -s)
  13. • Unpredictable external performance • Client side I/O • Server

    side I/O • Queue theory Twitter: @doubaokun
  14. Twitter: @doubaokun wait time utilization

  15. Twitter: @doubaokun Mapping real world problems onto machine with several

    levels abstraction with real world limitations in mind about management and structure
  16. Scaling up the power Twitter: @doubaokun

  17. Scaling up the power Twitter: @doubaokun

  18. = Scaling the concurrency Twitter: @doubaokun

  19. What is Concurrency? Twitter: @doubaokun

  20. Concurrency is managing multi-tasking Twitter: @doubaokun

  21. Concurrency is managing multi-tasking in both real world and machine

    world Twitter: @doubaokun
  22. Twitter: @doubaokun

  23. What is blocking Twitter: @doubaokun

  24. Twitter: @doubaokun

  25. Blocking is sequential Twitter: @doubaokun

  26. Blocking and sequential are the nature of human mind and

    machine Twitter: @doubaokun
  27. Blocking and sequential are the nature of a single Execution

    Unit Twitter: @doubaokun
  28. What is blocking I/O Twitter: @doubaokun

  29. Twitter: @doubaokun

  30. Blocking I/O is the nature of machine Twitter: @doubaokun

  31. What is async I/O Twitter: @doubaokun

  32. Twitter: @doubaokun

  33. The nature of real world I/O Twitter: @doubaokun

  34. The nature of real world is better resources utilisation Twitter:

    @doubaokun
  35. Mapping real world to machine Twitter: @doubaokun

  36. Mapping real world to machines: Unlimited Execution Unit and I/O

    vs Limited resources and machines Twitter: @doubaokun
  37. Concurrency is hard Twitter: @doubaokun

  38. Concurrency models • Process • Process pool • Thread •

    Thread pool • Single thread async I/O • Light weight thread: Fiber / Coroutine / … Twitter: @doubaokun
  39. Concurrency is hard • Inefficient scaling • Race condition Twitter:

    @doubaokun
  40. https://unixism.net/2019/04/linux-applications-performance-introduction/ Twitter: @doubaokun Performance of Concurrent I/O models

  41. Machine I/O Concurrency Twitter: @doubaokun

  42. Decoupling I/O and CPU Twitter: @doubaokun

  43. Async machine I/O Twitter: @doubaokun

  44. Twitter: @doubaokun

  45. Event based callbacks ? Twitter: @doubaokun

  46. not the nature of human mind Twitter: @doubaokun

  47. Async machine I/O should be abstracted Twitter: @doubaokun

  48. Understand the machine better Twitter: @doubaokun

  49. Concurrency and Operating System Twitter: @doubaokun

  50. What is a function? Twitter: @doubaokun

  51. a sequence of instructions that takes inputs and returns outputs

    Twitter: @doubaokun
  52. function add($a, $b) { return $a + $b; } Twitter:

    @doubaokun
  53. What is Context? Twitter: @doubaokun

  54. Twitter: @doubaokun

  55. Call Stack and Stack Frame Twitter: @doubaokun

  56. function add($a, $b) { return $a + $b; } function

    plus5($c, 5) { return add($c, 5); } Twitter: @doubaokun
  57. Twitter: @doubaokun

  58. What is context switch? Twitter: @doubaokun

  59. Twitter: @doubaokun

  60. History of multi-tasking in OS Twitter: @doubaokun

  61. 1. No scheduler Twitter: @doubaokun

  62. 2. Cooperative Scheduler Non-preemptive Twitter: @doubaokun

  63. 3. Preemptive timer interrupt scheduler Twitter: @doubaokun

  64. Twitter: @doubaokun

  65. Twitter: @doubaokun

  66. Thread context switch can be expensive Twitter: @doubaokun

  67. Not scalable Twitter: @doubaokun

  68. Light weight thread Twitter: @doubaokun

  69. Light weight Execution Unit Twitter: @doubaokun

  70. Light weight Execution Unit provided by machine/library Twitter: @doubaokun

  71. Light weight Execution Unit provided by machine to map real

    world problems Twitter: @doubaokun
  72. Light weight Execution Unit provided by machine to map real

    world problems to execute real world tasks Twitter: @doubaokun
  73. What is Coroutine? Twitter: @doubaokun

  74. “Coroutines are computer components that generalise subroutines for non-preemptive multitasking,

    by allowing execution to be suspended and resumed.” - Wikipedia Twitter: @doubaokun
  75. “a type of functions that enables concurrency via cooperative multitasking”

    Twitter: @doubaokun
  76. Cooperative multi-tasking Twitter: @doubaokun

  77. Multi-tasking management strategy Twitter: @doubaokun

  78. On machine: User space yield and resume Twitter: @doubaokun

  79. 1 3 start 2 4 Twitter: @doubaokun

  80. Twitter: @doubaokun

  81. Swoole Twitter: @doubaokun

  82. https://www.swoole.co.uk/ https://github.com/swoole/swoole-src @php_swoole Coroutine-based concurrency library with PHP syntax Twitter:

    @doubaokun
  83. Twitter: @doubaokun

  84. Super fast Twitter: @doubaokun

  85. Super fast https://tsh.io/blog/swoole-is-it-node-in-php-or-am-i-wrong/ Twitter: @doubaokun

  86. Super fast Twitter: @doubaokun

  87. Twitter: @doubaokun • Async I/O • Reuse states • Coroutine

    • Coroutine scheduler based on I/O
  88. Twitter: @doubaokun

  89. Swoole server Twitter: @doubaokun

  90. HTTP server with PHP Swoole Twitter: @doubaokun

  91. Server structure Twitter: @doubaokun

  92. Server structure Twitter: @doubaokun

  93. Swoole Coroutine Internal Twitter: @doubaokun

  94. Twitter: @doubaokun

  95. Use cases Twitter: @doubaokun

  96. Twitter: @doubaokun

  97. Common pitfalls Twitter: @doubaokun

  98. Twitter: @doubaokun Common pitfalls • Looking to run it on

    M$ Windows • Trying to run it with PHP-FPM • Stateless mind • Variable scope: super global, global, static, local variables • Blocking within Coroutine context • Exception handling • Exit
  99. Code samples Twitter: @doubaokun

  100. Coroutine Twitter: @doubaokun

  101. None
  102. 1 3 start 2 4 Coroutine yield and resume

  103. 2 1 3 2 Coroutine structure

  104. Server Twitter: @doubaokun

  105. Server response timeout

  106. Timer Twitter: @doubaokun

  107. Timer

  108. Client Twitter: @doubaokun

  109. HTTP Client

  110. Channel Twitter: @doubaokun

  111. None
  112. Coroutine Pooling Twitter: @doubaokun

  113. None
  114. None
  115. Linux process Twitter: @doubaokun

  116. None
  117. HashTable Twitter: @doubaokun

  118. None
  119. Task worker Twitter: @doubaokun

  120. None
  121. None
  122. Coroutine Server Twitter: @doubaokun

  123. Twitter: @doubaokun

  124. Demo Twitter: @doubaokun

  125. https://www.swoole.co.uk/ https://twitter.com/php_swoole https://github.com/swoole/swoole-src https://github.com/21days-dev/phpuk2020 Thank you