CJWorkbench / channels_rabbitmq

A Django Channels channel layer that uses RabbitMQ as its backing store
39 stars 17 forks source link

The caller tried using channels_rabbitmq on a different event loop than the one it was initialized with. #49

Open ruffish opened 1 year ago

ruffish commented 1 year ago

I don't really understand what is causing the issue.

I have my configuration setup like so:

# Channels Config
CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_rabbitmq.core.RabbitmqChannelLayer",
        "CONFIG": {
            "host": "HOSTURLREMOVED",
        },
    },
}

And I'm trying to call this:

# Broadcast trade to all connected clients via channel layer (websockets)
    try:
        from channels.layers import get_channel_layer
        channel_layer = get_channel_layer()

        await channel_layer.group_send(
            str(bot.id),  # The bot's id is used as the group name
            {
                'type': 'bot_trade',  # This matches the name of a method in the consumer
                'message': {MESSAGEREMOVED},
            }
        )
    except Exception as e:
        # Failed to broadcast trade
        print(e)

this is being called from a django view which executes a trade function, that trade function executes another function where this call is located.

an error always pops up in the terminal "The caller tried using channels_rabbitmq on a different event loop than the one it was initialized with."

Usually this error happens when trying to use django channels logic inside a celery function but I'm not.

Do you know what could be going wrong?

adamhooper commented 1 year ago

I see an await so I assume that means you're using Django async views....

In which case: big shrug from me. Your app is somehow using multiple event loops.

Try to produce a reduction: greatly simplify your app to make the error go away. Remove auth, and test again. Remove all other views, then test. Remove each middleware, testing after each. When the error goes away, it's probably from the most-recently-deleted code. Stare there and try to figure out what's up.

Worst case, you end up with just two or three files in your Django app -- and at this point, you'll have no business logic or personally identifiable information. If this mini-app still reports this message, please publish it as a GitHub project and link to it here.

Marking waiting for feedback :)

ruffish commented 1 year ago

So I was on a long break for a while from this project, and recently started working on it again. I'm in the process of testing this but it seems that the server I'm using to deploy eventually and test in development at the moment "daphne". Uses multiple cores to do concurrency. I locked the daphne server to only run on one core and from the print statements I added it seems like the problem could be caused by that. Switching threads essentially on different cores and that not working well with channels_rabbitMQ. I'll update upon further testing to let you know.