Skip to content

Commit

Permalink
Remove using usless threading call with async wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
vzhestkov committed Feb 2, 2024
1 parent fe80b71 commit 6aed402
Showing 1 changed file with 2 additions and 25 deletions.
27 changes: 2 additions & 25 deletions salt/utils/asynchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
Helpers/utils for working with tornado asynchronous stuff
"""


import asyncio
import contextlib
import logging
import sys
import threading

import tornado.concurrent
import tornado.ioloop
Expand Down Expand Up @@ -125,32 +122,12 @@ def __getattr__(self, key):

def _wrap(self, key):
def wrap(*args, **kwargs):
results = []
thread = threading.Thread(
target=self._target,
args=(key, args, kwargs, results, self.asyncio_loop),
return self.asyncio_loop.run_sync(
lambda: getattr(self.obj, key)(*args, **kwargs)
)
thread.start()
thread.join()
if results[0]:
return results[1]
else:
exc_info = results[1]
raise exc_info[1].with_traceback(exc_info[2])

return wrap

def _target(self, key, args, kwargs, results, asyncio_loop):
asyncio.set_event_loop(asyncio_loop)
io_loop = tornado.ioloop.IOLoop.current()
try:
result = io_loop.run_sync(lambda: getattr(self.obj, key)(*args, **kwargs))
results.append(True)
results.append(result)
except Exception: # pylint: disable=broad-except
results.append(False)
results.append(sys.exc_info())

def __enter__(self):
if hasattr(self.obj, "__aenter__"):
ret = self._wrap("__aenter__")()
Expand Down

0 comments on commit 6aed402

Please sign in to comment.