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

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

Open
ruffish opened this issue Aug 1, 2023 · 2 comments
Labels
question Further information is requested

Comments

@ruffish
Copy link

ruffish commented Aug 1, 2023

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
Copy link
Contributor

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 :)

@adamhooper adamhooper added the question Further information is requested label Aug 1, 2023
@ruffish
Copy link
Author

ruffish commented Sep 1, 2023

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.

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

No branches or pull requests

2 participants