Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. # package main
  2.  
  3. # import (
  4. # "bufio"
  5. # "time"
  6. # "fmt"
  7. # "os"
  8. # )
  9. import sys
  10. import time
  11. import asyncio
  12.  
  13.  
  14. def coroutinize(f, *args, **kwargs):
  15. return asyncio.get_event_loop().run_in_executor(None,
  16. lambda: f(*args,
  17. **kwargs))
  18.  
  19.  
  20. def go(f):
  21. return asyncio.ensure_future(f)
  22.  
  23. # func worker(c chan string) {
  24. # line := <-c
  25. # fmt.Println(line)
  26. # }
  27.  
  28.  
  29. async def worker(c):
  30. print("Worker waiting on channel")
  31. line = await c.get()
  32. print("Worker just read {}".format(line))
  33.  
  34. # func readLine(c chan string) {
  35. # reader := bufio.NewReader(os.Stdin)
  36. # line, err := reader.ReadString('\n')
  37. # fmt.Println(err)
  38. # c <- line
  39. # }
  40.  
  41. async def read_line(c):
  42. print("Read line waiting on stdin")
  43. line = await coroutinize(sys.stdin.readline)
  44. print("Read line sending '{}' to channel".format(line))
  45. await c.put(line)
  46.  
  47. # func doWork(end chan int) {
  48. # time.Sleep(2000 * time.Millisecond)
  49. # fmt.Println("do_work")
  50. # end <- 1
  51. # }
  52.  
  53.  
  54. async def do_work(end):
  55. print("Do work starts working")
  56. await coroutinize(time.sleep, 2)
  57. print("Do work sends somehting on the end channel")
  58. await end.put(1)
  59.  
  60. # func main() {
  61. # end := make(chan int)
  62. # c := make(chan string)
  63. # go worker(c)
  64. # go readLine(c)
  65. # go doWork(end)
  66. # <-end
  67. # }
  68.  
  69. async def main():
  70. end = asyncio.Queue(1)
  71. c = asyncio.Queue(1)
  72. print("Main launches worker")
  73. go(worker(c))
  74. print("Main launches readline")
  75. go(read_line(c))
  76. print('Main launches do_work')
  77. go(do_work(end))
  78. print("Main waits on end.")
  79. await end.get()
  80. print("Main quits.")
  81.  
  82.  
  83. if __name__ == '__main__':
  84. print("Bullshit code gets the event loop")
  85. loop = asyncio.get_event_loop()
  86. print("Bullshit code starts the event loop")
  87. loop.run_until_complete(main())
  88. print("Bullshit code exits")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement