class lazy(object):
def __init__(self, func):
self.func = func
def __get__(self, instance, cls):
val = self.func(instance)
setattr(instance, self.func.__name__, val)
return val
def lazy_property(func):
attr_name = "_lazy_" + func.__name__
@property
def _lazy_property(self):
if not hasattr(self, attr_name):
setattr(self, attr_name, func(self))
return getattr(self, attr_name)
return _lazy_property
Comparing with the lazy, it more like cache the result, maybe can also simply replaced by lru_cache. We have to ensure the return value won't change while using it.
Example of Async wait
import asyncio
from asyncio import Future
from typing import Dict
futures: Dict[str, Future] = {}
@app.get("/test/lock/{task_id}")
async def test_lock(task_id: str):
result = asyncio.get_event_loop().create_future()
# Put the task in Queue
futures[task_id] = result
return {
"result": await result
}
@app.get("/test/release/{task_id}")
async def test_release(task_id: str):
if task_id not in futures:
raise Exception("Task not found")
# Set result while the task finished
futures[task_id].set_result(f"{task_id} is done")
return {
"task": task_id
}
# This file is generated from information provided by
# the datasource. Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
version: 2
ethernets:
eth0:
optional: true
dhcp4: true
# add wifi setup information here ...
wifis:
wlan0:
optional: true
access-points:
"YOUR-SSID-NAME":
password: "YOUR-NETWORK-PASSWORD"
dhcp4: true
sudo netplan --debug try
# (continue even if there are errors)
sudo netplan --debug generate
# (provides more details in case of issues with the previous command)
sudo netplan --debug apply
# (if no issues during the previous commands)