代码
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
from threading import Lock
from redis import Redis
rdb = Redis(decode_responses=True)
pool = ThreadPoolExecutor(max_workers=10)
lock = Lock()
now = lambda: int(time.time())
def work():
lock.acquire()
utime = rdb.get("utime")
if utime and now() - int(utime) < 10:
lock.release()
return 200
else:
print("需要更新...")
time.sleep(2)
print("更新完成...")
utime = now()
rdb.set("utime", utime)
print("更新时间 ==> {}".format(time.strftime('%Y-%m-%d %H:%M.%S', time.localtime(utime))))
lock.release()
return 200
def callback(f):
print("回调函数 ==> {}".format(f.result()))
def run():
fs = []
for _ in range(10):
f = pool.submit(work)
fs.append(f)
for f in as_completed(fs):
print(f.result())
if __name__ == '__main__':
run()