Posted to tcl by aspect at Mon Jul 04 12:13:16 GMT 2016view raw

  1. # after [medranocalvo] - 2016-05-19 @ 1255
  2. #
  3. # using both idleproc and afterproc leads to weird behaviour:
  4. # afterproc never gets past the first call to [update]!
  5. proc idleproc {} {
  6. while 1 {
  7. set i [info cmdcount]
  8. #puts "$i: idleproc in [info coroutine]"
  9. after idle [list [info coroutine]]
  10. yield
  11. update idletasks
  12. }
  13. }
  14.  
  15. # unfortunately, this needs to be constantly putting itself
  16. # at the front of the event queue so that it can wake up
  17. # in time to process any events that have inserted themselves
  18. # since. So it never sleeps, which is a shame.
  19. proc afterproc {} {
  20. while 1 {
  21. set i [info cmdcount]
  22. puts "$i: afterproc in [info coroutine]: yielding"
  23. after 0 [list [info coroutine]]
  24. yield
  25. puts "$i: afterproc in [info coroutine]: after yield"
  26. update
  27. puts "$i: afterproc in [info coroutine]: after update"
  28. }
  29. }
  30.  
  31.  
  32. #coroutine idlecoro idleproc
  33. coroutine aftercoro afterproc
  34.  
  35. after idle {
  36. puts i:[info coroutine]
  37. after idle {puts ii:[info coroutine]}
  38. }
  39. after idle {after 0 {puts i0:\ [info coroutine]}}
  40. after 0 {after idle {puts 0i:\ [info coroutine]}}
  41. after 0 {puts 0:[info coroutine]}
  42. after 100 {
  43. after idle {
  44. puts i:[info coroutine]
  45. after idle {puts Xii:\ [info coroutine]}
  46. }
  47. after idle {after 100 {puts XiX:\ [info coroutine]}}
  48. after 100 {after idle {puts XXi:\ [info coroutine]}}
  49. after 100 {puts XX:\ [info coroutine]}
  50. }
  51.  
  52. set s [socket -async google.com 443]
  53. fileevent $s writable [list apply {{s args} {
  54. puts "writable: [info coroutine]"
  55. close $s
  56. }} $s]
  57.  
  58. vwait forever
  59.