Skip to content

Commit

Permalink
save event_loop to thread.local
Browse files Browse the repository at this point in the history
  • Loading branch information
keijack committed Oct 9, 2022
1 parent ec9bec6 commit 9d48b62
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
2 changes: 1 addition & 1 deletion py_eureka_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
SOFTWARE.
"""

version = "0.11.3"
version = "0.11.4"

"""
Status of instances
Expand Down
17 changes: 8 additions & 9 deletions py_eureka_client/eureka_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import json
import re
import socket
import threading
import time

import random
Expand All @@ -48,7 +49,7 @@
from py_eureka_client import HA_STRATEGY_RANDOM, HA_STRATEGY_STICK, HA_STRATEGY_OTHER
from py_eureka_client import ERROR_REGISTER, ERROR_DISCOVER, ERROR_STATUS_UPDATE
from py_eureka_client import _DEFAULT_EUREKA_SERVER_URL, _DEFAULT_INSTNACE_PORT, _DEFAULT_INSTNACE_SECURE_PORT, _RENEWAL_INTERVAL_IN_SECS, _RENEWAL_INTERVAL_IN_SECS, _DURATION_IN_SECS, _DEFAULT_DATA_CENTER_INFO, _DEFAULT_DATA_CENTER_INFO_CLASS, _AMAZON_DATA_CENTER_INFO_CLASS
from py_eureka_client import _DEFAULT_ENCODING, _DEFAUTL_ZONE, _DEFAULT_TIME_OUT
from py_eureka_client import _DEFAUTL_ZONE, _DEFAULT_TIME_OUT

from py_eureka_client.eureka_basic import LeaseInfo, DataCenterInfo, PortWrapper, Instance, Application, Applications
from py_eureka_client.eureka_basic import register, _register, cancel, send_heartbeat, status_update, delete_status_override
Expand Down Expand Up @@ -1190,24 +1191,22 @@ async def stop_async() -> None:
if client is not None:
await client.stop()

_event_loop: asyncio.AbstractEventLoop = None
_thread_local = threading.local()


def set_event_loop(event_loop: asyncio.AbstractEventLoop):
global _event_loop
if not isinstance(event_loop, asyncio.AbstractEventLoop):
raise Exception("You must set an even loop object into this.")
_event_loop = event_loop
_thread_local.event_loop = event_loop


def get_event_loop() -> asyncio.AbstractEventLoop:
global _event_loop
if not _event_loop:
if not hasattr(_thread_local, "event_loop"):
try:
_event_loop = asyncio.new_event_loop()
_thread_local.event_loop = asyncio.new_event_loop()
except:
_event_loop = asyncio.get_event_loop()
return _event_loop
_thread_local.event_loop = asyncio.get_event_loop()
return _thread_local.event_loop


def init(eureka_server: str = _DEFAULT_EUREKA_SERVER_URL,
Expand Down

0 comments on commit 9d48b62

Please sign in to comment.