Using async changes thread for non-async callbacks which is unexpected
Ninguém assumiu esta issue ainda.
Avaliação
- Dificuldade
- 4/5
- Tempo estimado
- 3-5 dias
- Facilidade para iniciantes
- 52/100
- Tipo de issue
- Bug
- Clareza
- Razoavelmente clara
- Status de atividade
- Pouca atividade
- Stack de tecnologia
- python
- Domínio
- networking
Direção de pesquisa
Comece por can/notifier.py nas linhas referenciadas de _rx_thread() e rastreie _on_message_received() junto com o caminho de agendamento do asyncio. Revise as issues #1938 e #1912 juntamente com esta alteração. Considera-se concluído quando callbacks não async puderem permanecer na thread de recebimento do CAN quando configurados dessa forma, enquanto callbacks async forem agendados com segurança a partir de qualquer uma das threads, sem alterar o comportamento padrão.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
Using the can library with async enabled Notifier change the thread which all callbacks is called from. Without async, the notifier callbacks are run in the thread of the can rx thread. With async, it runs the callbacks via loop.call_soon_threadsafe() which cause the notifier callbacks to be run in the main async thread instead. This changes behavior for all callbacks, not just async callbacks, which is unexpected. E.g. if using a non-async can protocol stack plus another async protocol stack together might create problems due to the change of callback threads.
I propose a fix that prevents the async call_soon_threadsafe in Notifier._rx_thread() with the introduction of a setting run_message_reception_in_eventloop. Then the user can chose to opt out of running the regular non-async callbacks in the asyncio main thread. The default should be True so we don't change the current behavior.
def _rx_thread(self, bus: BusABC) -> None:
# determine message handling callable early, not inside while loop
if self._loop and self.run_message_reception_in_eventloop: # <--- NEW
handle_message: Callable[[Message], Any] = functools.partial(
self._loop.call_soon_threadsafe,
self._on_message_received, # type: ignore[arg-type]
)
else:
handle_message = self._on_message_received
A change is required in Notifier._on_message_received(). This function might be called in either threads, so calling the asyncio coroutine must be made thread safe:
def _on_message_received(self, msg: Message) -> None:
for callback in self.listeners:
res = callback(msg)
if res and self._loop and asyncio.iscoroutine(res):
# Schedule coroutine
# In __init__: self._tasks: set[asyncio.Task] = set()
task = asyncio.run_coroutine_threadsafe(res, self._loop)
When looking at this, #1938 and #1912 should be addressed in the same round.
- Linguagem predominante
- Python
- Estrelas
- 1.6k
- Forks
- 697
- Métricas de merge de PRs
- Nenhum PR com merge em 30d
Guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de hardbyte/python-can
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 88/100
hardbyte/python-can#2103 ·
-
bug
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 78/100
hardbyte/python-can#2077 · 1 comentário · 1 reação ·
-
bug
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 68/100
hardbyte/python-can#1922 · 1 reação ·
-
enhancement
Dificuldade 5/5 Mais de uma semana Facilidade para iniciantes 30/100
hardbyte/python-can#2102 ·
-
bug
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 68/100
hardbyte/python-can#2092 ·
Todas as issues de hardbyte/python-can
Issues semelhantes
-
area: harness bug status: needs-triage
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 75/100
Human-Agent-Society/reef#625 ·
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 70/100
-
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 80/100
learningequality/kolibri#15351 · 2 comentários ·
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 75/100
-
Name consistency Aberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 75/100
eellak/triplestore#65 · 1 comentário ·