一个纯 Python3 实现的轻量且易于使用的跨网络队列服务。
此项目主要分为 client 端和 server 端,队列数据保存于 server,同时 server 和 client 可以同时操作队列,支持多个 client,本地队列是基于 python3 标准库 queue 实现,项目核心主要是基于 TCP 长连接进行跨网络传输队列数据,同时确保 client 的操作是线程安全的。
特点 :
例子 :
server.py
from wukongqueue import WuKongQueue import time # start a queue server svr = WuKongQueue(host='127.0.0.1', port=666, max_conns=10, max_size=0) with svr: print("svr is started!") svr.put(b"1") svr.put(b"2") print("putted b'1' and b'2', wait for clients...") time.sleep(10) print("svr closed!")
clientA.py
from wukongqueue import WuKongQueueClient client = WuKongQueueClient(host='127.0.0.1', port=666) with client: print("got", client.get()) # b"1" client.task_done() import time wait = 5 time.sleep(wait) print("after %s seconds, got" % wait, client.get(block=True)) # wait for a while, then print b"2" client.task_done() print("clientA: all task done!")
clientB.py
from wukongqueue import WuKongQueueClient client = WuKongQueueClient(host='127.0.0.1', port=666) with client: client.join() print("clientB all task done!")
按上面的顺序启动三个程序,可以看到如下效果:
# server.py 首先打印 svr is started! (马上) putted b'1' and b'2', wait for clients... (马上) svr closed! (10秒后) # clientA print secondly got b'1' (马上) after 5 seconds, got b'2' (5秒后) clientA: all task done! (马上) # clientB print lastly clientB all task done! (与clientA的all task done同步)
连接池:
from wukongqueue import ConnectionPool,WuKongQueueClient pool = ConnectionPool(host=”localhost”, port=2020, max_connections=3) client = WuKongQueueClient(connection_pool=pool)
暂不支持的功能(也是TODO) :
我可以使用吗 :此项目自诞生起就引入我的一个大型分布式爬虫项目中,目前持续运行中,项目至今迭代了6个版本。
后 :连接池设计部分受 redis库 启发,读者如有兴趣可阅读源码与我讨论~
希望大家能够关注一下,给予我持久更新此项目的动力^_^