Когда я использую потоки для выполнения API агента соли, я всегда получаю сообщение об ошибке RuntimeError: в потоке «ThPython

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Когда я использую потоки для выполнения API агента соли, я всегда получаю сообщение об ошибке RuntimeError: в потоке «Th

Сообщение Anonymous »

Когда я использую потоки для выполнения salt.client.get_local_client() и одновременно вызываю функцию cmd(), я получаю ошибку об отсутствии цикла событий в потоке. Я не уверен, связана ли проблема с моим подходом или с дизайном самой соли.

Код: Выделить всё

python test.py
Exception in thread Thread-2:
Traceback (most recent call last):
Exception in thread Thread-1:
File "/home/test/.pyenv/versions/test/lib/python3.9/site-packages/salt/client/__init__.py", line 387, in run_job
Traceback (most recent call last):
File "/home/test/.pyenv/versions/test/lib/python3.9/site-packages/salt/client/__init__.py", line 387, in run_job
pub_data = self.pub(
File "/home/test/.pyenv/versions/test/lib/python3.9/site-packages/salt/client/__init__.py", line 1898, in pub
pub_data = self.pub(
File "/home/test/.pyenv/versions/test/lib/python3.9/site-packages/salt/client/__init__.py", line 1898, in pub
with salt.channel.client.ReqChannel.factory(
File "/home/test/.pyenv/versions/test/lib/python3.9/site-packages/salt/channel/client.py", line 56, in factory
with salt.channel.client.ReqChannel.factory(
return SyncWrapper(
File "/home/test/.pyenv/versions/test/lib/python3.9/site-packages/salt/channel/client.py", line 56, in factory
File "/home/test/.pyenv/versions/test/lib/python3.9/site-packages/salt/utils/asynchronous.py", line 76, in __init__
return SyncWrapper(
File "/home/test/.pyenv/versions/test/lib/python3.9/site-packages/salt/utils/asynchronous.py", line 76, in __init__
self.obj = cls(*args, **kwargs)
File "/home/test/.pyenv/versions/test/lib/python3.9/site-packages/salt/channel/client.py", line 138, in factory
self.obj = cls(*args, **kwargs)
File "/home/test/.pyenv/versions/test/lib/python3.9/site-packages/salt/channel/client.py", line 138, in factory
transport = salt.transport.request_client(opts, io_loop=io_loop)
File "/home/test/.pyenv/versions/test/lib/python3.9/site-packages/salt/transport/base.py", line 59, in request_client
transport = salt.transport.request_client(opts, io_loop=io_loop)
File "/home/test/.pyenv/versions/test/lib/python3.9/site-packages/salt/transport/base.py", line 59, in request_client
return salt.transport.zeromq.RequestClient(opts, io_loop=io_loop)
File "/home/test/.pyenv/versions/test/lib/python3.9/site-packages/salt/transport/zeromq.py", line 1084, in __init__
return salt.transport.zeromq.RequestClient(opts, io_loop=io_loop)
File "/home/test/.pyenv/versions/test/lib/python3.9/site-packages/salt/transport/zeromq.py", line 1084, in __init__
self.sending = asyncio.Lock()
File "/home/test/.pyenv/versions/3.9.18/lib/python3.9/asyncio/locks.py", line 81, in __init__
self.sending = asyncio.Lock()
File "/home/test/.pyenv/versions/3.9.18/lib/python3.9/asyncio/locks.py", line 81, in __init__
self._loop = events.get_event_loop()
File "/home/test/.pyenv/versions/3.9.18/lib/python3.9/asyncio/events.py", line 642, in get_event_loop
self._loop = events.get_event_loop()
File "/home/test/.pyenv/versions/3.9.18/lib/python3.9/asyncio/events.py", line 642, in get_event_loop
raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-2'.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/test/.pyenv/versions/3.9.18/lib/python3.9/threading.py", line 980, in _bootstrap_inner
raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError:  There is no current event loop in thread 'Thread-1'.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/test/.pyenv/versions/3.9.18/lib/python3.9/threading.py", line 980, in _bootstrap_inner
self.run()
File "/home/test/.pyenv/versions/3.9.18/lib/python3.9/threading.py", line 917, in run
self.run()
File "/home/test/.pyenv/versions/3.9.18/lib/python3.9/threading.py", line 917, in run
self._target(*self._args, **self._kwargs)
File "/home/test/test.py", line 9, in test_sleep
self._target(*self._args, **self._kwargs)
cl.cmd('master-local', 'test.sleep', [2])
File "/home/test/test.py", line 9, in test_sleep
File "/home/test/.pyenv/versions/test/lib/python3.9/site-packages/salt/client/__init__.py", line 752, in cmd
cl.cmd('master-local', 'test.sleep', [2])
File "/home/test/.pyenv/versions/test/lib/python3.9/site-packages/salt/client/__init__.py", line 752, in cmd
pub_data = self.run_job(
File "/home/test/.pyenv/versions/test/lib/python3.9/site-packages/salt/client/__init__.py", line 409, in run_job
pub_data = self.run_job(
File "/home/test/.pyenv/versions/test/lib/python3.9/site-packages/salt/client/__init__.py", line 409, in run_job
raise SaltClientError(general_exception)
salt.exceptions.SaltClientError: There is no current event loop in thread 'Thread-2'.
raise SaltClientError(general_exception)
salt.exceptions.SaltClientError: There is no current event loop in thread 'Thread-1'.
Ниже приведен мой тестовый код

Код: Выделить всё

from threading import Thread

import salt.client
import time

cl = salt.client.get_local_client()

def test_sleep():
cl.cmd('master-local', 'test.sleep', [2])

t1 = Thread(target=test_sleep)
t2 = Thread(target=test_sleep)

t1.start()
# time.sleep(1)  # 
Как я могу правильно достичь одновременных вызовов, используя потоки с помощью API клиента Salt? Это ограничение в дизайне Salt, или что -то не так с моей реализацией?  

Подробнее здесь: [url]https://stackoverflow.com/questions/79388267/when-i-use-threads-to-execute-the-salt-agent-api-i-always-get-an-error-runtimee[/url]
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»