DEV Community

Vee Satayamas
Vee Satayamas

Posted on

Web server กับ process thread และ event loop อย่างสั้น

6 มีนาคม 2562

ผมจะใช้คำศัพท์ thread และ process ในบริบทของ Linux และ *BSD นะครับ ใน OS อื่น ๆ บางตัวเรียกไม่เหมือนกัน

nginx และ thread pool

  • nginx ใช้ event loop ด้วย ใช้หลาย thread ด้วยครับ 1
  • ผมเข้าใจว่าใช้ event loop เดียวรับ request แล้วไปปล่อย task เข้า thread อื่นอีกที แล้วส่งผลคิน event loop

node.js cluster

  • node.js cluster แตก process (แทนที่จะแตก thread) ครับ มี process หลักมา accept incoming connection แล้วส่งต่อให้ process ลูกครับ2
  • แต่ว่าไม่แน่ใจว่ามี event loop ของแต่ละ process อีกทีหรือเปล่าครับ ผมเดาว่าน่าจะมีแต่ก็ไม่เคยลองดูจริง ๆ ครับ

Tokio

  • Tokio อย่างน้อยจะต่างจาก node cluster ตรงที่ไม่ได้แตก process ครับ แต่ว่าใช้แตก thread ในแง่นี้น่าจะคล้าย nginx มากกว่า node cluster
  • ข้อมูลอีกอย่างคือมี event loop เท่าจำนวน thread ที่แตกออกมาครับ แต่ส่วนนี้ผมยังไม่ได้ดูว่าแบ่งงานกันอย่างไรครับ

CGI

ส่วนพวก CGI คือแตก process (ไม่ใช่ thread) ทุกครั้งที่มี request ครับ

PHP

  • php-fpm แตก process มารับ request
  • mod_php บน Apache ก็แตก process เหมือนกันครับ เพราะว่า php ต้องการแบ่งแยกระดับ process
  • พวกนี้ก็จะทำให้เร็วขึ้นโดยใช้ prefork ไว้ก่อนทำให้เร็วขึ้นได้
  • ใช้ process pool ไม่ได้ fork หมดเหมือน CGI แต่ก็เปลือง RAM อยู่ดีครับ
  • นอกจากนั้นเพื่อที่จะทำให้เร็วขึ้นมันมี opcache ที่ compile โปรแกรมเก็บไว้ก่อนรับ request อยู่แล้วครับ

Gunicorn และ Meinheld

gnuicorn ใช้ process มาเป็น worker โดยที่ worker ที่ใช้ meinheld ข้างในก็ไปใช้ picoev เอามาทำ event loop อีกที

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay