README.rst 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. ==================
  2. Eventloop examples
  3. ==================
  4. Overview and usage
  5. ==================
  6. A few examples on how an event loop can be implemented with Duktape, mainly
  7. illlustrating how the Duktape interface works (not how event loops should be
  8. built otherwise).
  9. To test (Linux only, perhaps other Unix)::
  10. $ make
  11. $ ./evloop curses-timers.js # run with Ecmascript eventloop
  12. $ ./evloop -c curses-timers.js # run with C eventloop
  13. Implementation approaches
  14. =========================
  15. There are several approaches to implementation timers. Here we demonstrate
  16. two main approaches:
  17. 1. Using a C eventloop which calls into Javascript. All the event loop state
  18. like timers, sockets, etc, is held in C structures.
  19. (See ``c_eventloop.c`` and ``c_eventloop.js``.)
  20. 2. Using an Ecmascript eventloop which never returns. All the event loop state
  21. can be managed with Ecmascript code instead of C structures. The Ecmascript
  22. eventloop calls a Duktape/C helper to do the lowest level poll() call.
  23. (See ``ecma_eventloop.js``.)
  24. Services provided
  25. =================
  26. The event loop API provided by both examples is the same, and includes:
  27. * Timers: setTimeout, clearTimeout, setInterval, clearInterval
  28. * Sockets: simple network sockets
  29. In addition there are a few synchronous API bindings which are not event loop
  30. related:
  31. * File I/O
  32. * Curses, for doing beautiful character graphics
  33. Limitations
  34. ===========
  35. This is **not** a production quality event loop. This is on purpose, to
  36. keep the example somewhat simple. Some shortcomings include:
  37. * A production quality event loop would track its internal state (active
  38. timers and sockets) much more efficiently. In general memory usage and
  39. code footprint can be reduced.
  40. * Buffer churn caused by allocating a new buffer for every socket read
  41. should be eliminated by reusing buffers where appropriate. Although
  42. churn doesn't increase memory footprint with reference counting, it
  43. is slower than reusing buffers and might increase memory fragmentation.
  44. * There is no way to suspend reading or writing in the example. Adding
  45. them is straightforward: the poll set needs to be managed dynamically.
  46. * The example uses poll() while one should use epoll() on Linux, kqueue()
  47. on BSD systems, etc.
  48. * Timers are not very accurate, e.g. setInterval() does not try to guarantee
  49. a steady schedule. Instead, the next interval is scheduled after the
  50. current callback has finished. This is not the best behavior for some
  51. environments, but avoids bunching callbacks.
  52. * Error handling is mostly missing. Debug prints don't interact well
  53. with curses.