Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'uvicorn' in functional components in Python. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
return cursor.fetchone()
@app.delete("/items/{item_id}")
def delete_item(item_id: int):
with CONNECTION.cursor() as cursor:
cursor.execute('DELETE FROM my_table WHERE num=%s RETURNING *;', (item_id, ))
CONNECTION.commit()
return cursor.fetchone()
if __name__ == "__main__":
try:
CONNECTION = psycopg2.connect(dbname='postgres', user='postgres', host='my_db', port=5432)
create_database()
uvicorn.run(app, host="0.0.0.0", port=5000, log_level="info")
finally:
CONNECTION.close()
async def test():
config = Config(app=app, lifespan="auto")
lifespan = LifespanOn(config)
assert not startup_complete
assert not shutdown_complete
await lifespan.startup()
assert startup_complete
assert not shutdown_complete
await lifespan.shutdown()
assert startup_complete
assert shutdown_complete
async def test():
config = Config(app=app, lifespan="on")
lifespan = LifespanOn(config)
assert not startup_complete
assert not shutdown_complete
await lifespan.startup()
assert startup_complete
assert not shutdown_complete
await lifespan.shutdown()
assert startup_complete
assert shutdown_complete
async def test():
config = Config(app=app, lifespan="auto")
lifespan = LifespanOn(config)
await lifespan.startup()
assert lifespan.error_occured
assert not lifespan.should_exit
await lifespan.shutdown()
async def test():
config = Config(app=app, lifespan="on")
lifespan = LifespanOn(config)
assert not startup_complete
assert not shutdown_complete
await lifespan.startup()
assert startup_complete
assert not shutdown_complete
await lifespan.shutdown()
assert startup_complete
assert shutdown_complete
async def test():
config = Config(app=app, lifespan="on")
lifespan = LifespanOn(config)
await lifespan.startup()
assert lifespan.error_occured
assert lifespan.should_exit
await lifespan.shutdown()
async def test():
config = Config(app=app, lifespan="auto")
lifespan = LifespanOn(config)
assert not startup_complete
assert not shutdown_complete
await lifespan.startup()
assert startup_complete
assert not shutdown_complete
await lifespan.shutdown()
assert startup_complete
assert shutdown_complete
def test_ssl_config(certfile_and_keyfile):
certfile, keyfile = certfile_and_keyfile
config = Config(app=asgi_app, ssl_certfile=certfile, ssl_keyfile=keyfile)
config.load()
assert config.is_ssl is True
def test_wsgi_app():
config = Config(app=wsgi_app, interface="wsgi", proxy_headers=False)
config.load()
assert isinstance(config.loaded_app, WSGIMiddleware)
assert config.interface == "wsgi"
def test_run():
class App:
def __init__(self, scope):
if scope["type"] != "http":
raise Exception()
async def __call__(self, receive, send):
await send({"type": "http.response.start", "status": 204, "headers": []})
await send({"type": "http.response.body", "body": b"", "more_body": False})
class CustomServer(Server):
def install_signal_handlers(self):
pass
config = Config(app=App, loop="asyncio", limit_max_requests=1)
server = CustomServer(config=config)
thread = threading.Thread(target=server.run)
thread.start()
while not server.started:
time.sleep(0.01)
response = requests.get("http://127.0.0.1:8000")
assert response.status_code == 204
thread.join()