From b54829d417528c7249585f8235bec829f3cb73b1 Mon Sep 17 00:00:00 2001 From: Hoang Viet Date: Thu, 8 Dec 2022 14:23:07 +0700 Subject: [PATCH 1/2] Improve mlchain_context --- mlchain/__init__.py | 2 +- mlchain/server/flask_server.py | 1 + mlchain/server/starlette_server.py | 1 + mlchain/server/view.py | 4 +++- setup.py | 2 +- 5 files changed, 7 insertions(+), 3 deletions(-) diff --git a/mlchain/__init__.py b/mlchain/__init__.py index 83be48f..90cac33 100644 --- a/mlchain/__init__.py +++ b/mlchain/__init__.py @@ -27,7 +27,7 @@ ) # Parameters of MLchain -__version__ = "0.3.1" +__version__ = "0.3.2" HOST = "https://www.api.mlchain.ml" WEB_HOST = HOST diff --git a/mlchain/server/flask_server.py b/mlchain/server/flask_server.py index 5c5120f..31a493f 100644 --- a/mlchain/server/flask_server.py +++ b/mlchain/server/flask_server.py @@ -74,6 +74,7 @@ def __get_msgpack_blosc_response(self, output, status=200): def init_context(self): uid = str(uuid4()) + mlchain_context.set_mlchain_context_id(uid) mlchain_context['MLCHAIN_CONTEXT_ID'] = uid return uid diff --git a/mlchain/server/starlette_server.py b/mlchain/server/starlette_server.py index cab3907..52126d1 100644 --- a/mlchain/server/starlette_server.py +++ b/mlchain/server/starlette_server.py @@ -79,6 +79,7 @@ def __get_msgpack_blosc_response(self, output, status=200): def init_context(self): uid = str(uuid4()) + mlchain_context.set_mlchain_context_id(uid) mlchain_context['MLCHAIN_CONTEXT_ID'] = uid return uid diff --git a/mlchain/server/view.py b/mlchain/server/view.py index da48dab..4dc8d82 100644 --- a/mlchain/server/view.py +++ b/mlchain/server/view.py @@ -52,14 +52,16 @@ def init_context_with_headers(self, headers, context_id:str = None): mlchain_context.set(context) if mlchain_context.MLCHAIN_CONTEXT_ID is None: + mlchain_context.set_mlchain_context_id(context_id) mlchain_context['MLCHAIN_CONTEXT_ID'] = context_id else: - context_id = mlchain_context['MLCHAIN_CONTEXT_ID'] + context_id = mlchain_context.MLCHAIN_CONTEXT_ID return context_id def init_context(self): uid = str(uuid4()) + mlchain_context.set_mlchain_context_id(uid) mlchain_context['MLCHAIN_CONTEXT_ID'] = uid return uid diff --git a/setup.py b/setup.py index f4a002d..6ac3d90 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ import os from setuptools import setup, find_packages -__version__ = "0.3.1" +__version__ = "0.3.2" project = "mlchain" From 673dc09b12a6f31851c0746e212aa16f6c4f9237 Mon Sep 17 00:00:00 2001 From: Hoang Viet Date: Thu, 8 Dec 2022 14:52:41 +0700 Subject: [PATCH 2/2] Add mlchain context to before gevent --- mlchain/__init__.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/mlchain/__init__.py b/mlchain/__init__.py index 90cac33..2179151 100644 --- a/mlchain/__init__.py +++ b/mlchain/__init__.py @@ -1,5 +1,15 @@ from os import environ +# Mlchain Context +from contextvars import ContextVar +from typing import Any, Dict + +_request_scope_context_storage: ContextVar[Dict[Any, Any]] = ContextVar( + "mlchain_context" +) +from .context import mlchain_context + +# Gevent fix if "DISABLE_GEVENT_FIX" not in environ: # Fix gevent try: @@ -18,13 +28,6 @@ # Handle target environment that doesn't support HTTPS verification ssl._create_default_https_context = _create_unverified_https_context -# Mlchain Context -from contextvars import ContextVar -from typing import Any, Dict - -_request_scope_context_storage: ContextVar[Dict[Any, Any]] = ContextVar( - "mlchain_context" -) # Parameters of MLchain __version__ = "0.3.2" @@ -34,12 +37,9 @@ API_ADDRESS = HOST MODEL_ID = None -from os import environ - environ['OBJC_DISABLE_INITIALIZE_FORK_SAFETY'] = 'YES' from mlchain.base.log import logger -from .context import mlchain_context from .base.exceptions import * from .config import mlconfig