-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasyncio06redis.py
60 lines (37 loc) · 1.17 KB
/
asyncio06redis.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
# -*- coding -*-
import time
import asyncio
import redis
from queue import Queue
from threading import Thread
def start_loop(loop):
asyncio.set_event_loop(loop)
loop.run_forever()
async def do_sleep(x, q, m=""):
await asyncio.sleep(x)
q.put(m)
def get_redis():
connection_pool = redis.ConnectionPool(host='127.0.0.1', password=123456, port=6379)
return redis.Redis(connection_pool=connection_pool)
def consumer(rcon, queue, new_loop):
while True:
task = rcon.rpop("queue")
if not task:
time.sleep(1)
continue
asyncio.run_coroutine_threadsafe(do_sleep(int(task), queue), new_loop)
if __name__ == '__main__':
print(time.ctime())
new_loop = asyncio.new_event_loop()
loop_thread = Thread(target=start_loop, args=(new_loop, ))
loop_thread.setDaemon(True)
loop_thread.start()
rcon = get_redis()
queue = Queue()
consumer_thread = Thread(target=consumer, args=(rcon, queue, new_loop,))
consumer_thread.setDaemon(True)
consumer_thread.start()
while True:
msg = queue.get()
print("协程运行完..")
print("当前时间:", time.ctime())