Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call publish from a different thread #224

Open
JochenKienzle opened this issue Nov 24, 2020 · 3 comments
Open

Call publish from a different thread #224

JochenKienzle opened this issue Nov 24, 2020 · 3 comments

Comments

@JochenKienzle
Copy link

Hello,

I have issues with mqtt_client.publish_json when it is called from a different thread:


class SomeApp:
    # will be called with a loop using asyncio
    def __init__(self, client_id, connection_string):
        #Init and connect MqttClient

    # Called from a different thread without any loop
    def __on_event(self, ev):
        asyncio.run(self.client.publish(topic, msg))

This is working but sometimes get the error the following error:

Future <Future pending> attached to a different loop

I tried the following:

    def __on_event(self, ev):
        loop = asyncio.new_event_loop()
        loop.run_until_complete(mqtt_client.publish(topic, msg))

but the same error occurs sometimes.

If I'm using

    def __on_event(self, topic, msg):
        loop = self.running_publish_loop # This is a running loop in a separate thread only for publishing
        loop.run_coroutine_threadsafe(mqtt_client.publish(topic, msg))

then it results sometimes in an Assertion error:

File "...\AppData\Local\Programs\Python\Python38-32\lib\asyncio\proactor_events.py", line 375, in _loop_writing
    assert f is self._write_fut

Now, I don't know how to call publish() from a different thread without facing these issues. Could someone help me on this topic?

Many thanks in advance

@FlorianLudwig
Copy link
Contributor

This library is meant to be used with asyncio and is not thread safe. So this is expected.

It is generally not a good idea to mix threading and asyncio. So to fix your issue please look into asyncio how to use it to replace your threading needs.

@streamsunshine
Copy link

have you found the way to solve this problem? i have the same confusion with fastapi websocket @JochenKienzle

@JochenKienzle
Copy link
Author

@streamsunshine
Unfortunately, no. We changed our approach to avoid this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants