-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
68 lines (53 loc) · 1.4 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# from utils.datatool import RemoteIO
#
# remoteio = RemoteIO("localhost")
#
# remoteio.test(1)
# import os
# os.chdir("PycharmProjects/tornado-learning")
# from translate.app.biz import sleeeep
# @gen.coroutine
# def sleeeep(args):
# print("i'm sleeping")
# time.sleep(10)
# print("wake up")
# for next in args:
# if next != "m":
# yield "in the sleep %s" % next
# else:
# print(1)
from tornado import gen
from tornado.ioloop import IOLoop
from tornado.queues import Queue
q = Queue(maxsize=2)
@gen.coroutine
def consumer():
count = 0
while True:
if q.empty() and count == 1:
print("stop")
count -= 1
else:
count = q.qsize()
item = yield q.get()
print('Doing work on %s' % item)
yield gen.sleep(0.5)
@gen.coroutine
def producer():
for item in range(5):
yield q.put(item)
print('Put %s' % item)
for item in range(5):
yield q.put(item)
print('Put %s' % item)
for item in range(5):
q.put(item)
@gen.coroutine
def main():
# Start consumer without waiting (since it never finishes).
IOLoop.current().spawn_callback(consumer)
# yield producer() # Wait for producer to put all tasks.
print("abc")
yield q.join() # Wait for consumer to finish all tasks.
print('Done')
IOLoop.current().run_sync(main)