#!/usr/bin/env python
#-*- coding:utf-8 -*-
import time
import redis
conn = redis.Redis()
# 用户端发起派车请求
def request_cab(user_id, priority):
# 请在下面完成要求的功能
#********* Begin *********#
if conn.hexists('request:info:' + str(user_id), 'time'):
return
pipe = conn.pipeline()
pipe.lpush('cab:queue', user_id)
pipe.hmset('request:info:' + str(user_id), {'time': time.time(), 'priority': priority})
pipe.expire('request:info:' + str(user_id), 10 * 60)
pipe.execute()
#********* End *********#
# 平台选择优先级最高的派车请求并派车
def allocate():
# 请在下面完成要求的功能
#********* Begin *********#
cab_queue = conn.sort('cab:queue', by='request:info:*->priority', desc=True)
current_respond = cab_queue[0]
conn.lrem('cab:queue', current_respond, 1)
return current_respond
#********* End *********#
# 用户端取消派车请求
def cancel_cab(user_id):
conn.expire('request:info:' + str(user_id), 0)
conn.lrem('cab:queue', user_id)
01-06
3208