首页 > 解决方案 > UWSGI 没有名为 uwsgi 的模块

问题描述

我希望你做得很好。我有问题 import uwsgi 感谢您的帮助,我的 rabbitmq 正在幕后工作,不用担心

我已经安装了版本 2.0.15 和 2.0.18 的 uwsgi,但我尝试了它仍然不起作用我的目的是导入 uwsgi 和

uwsgi.websocket_handshake(
        env['HTTP_SEC_WEBSOCKET_KEY'],
        env.get('HTTP_ORIGIN', '')
    )

如果我不能导入 uwsgi 所以我不能使用 uwsgi.websocker_handshake

"""Receive messages over from RabbitMQ and send them over the websocket."""

import sys

import pika
import uwsgi


def application(env, start_response):
    """Setup the Websocket Server and read messages off the queue."""
    connection = pika.BlockingConnection(
    pika.ConnectionParameters(host='localhost')
    )
    channel = connection.channel()

    exchange = env['PATH_INFO'].replace('/', '')

    channel.exchange_declare(
        exchange=exchange, exchange_type='fanout'
    )

    # exclusive means the queue should be deleted once the connection is closed
    result = channel.queue_declare(exclusive=True)
    queue_name = result.method.queue  # random queue name generated by RabbitMQ

    channel.queue_bind(exchange=exchange, queue=queue_name)

    uwsgi.websocket_handshake(
        env['HTTP_SEC_WEBSOCKET_KEY'],
        env.get('HTTP_ORIGIN', '')
    )

    def keepalive():
        """Keep the websocket connection alive (called every 30 seconds)."""
        print('PING/PONG...')
        try:
            uwsgi.websocket_recv_nb()
            connection.add_timeout(30, keepalive)
        except OSError as error:
            connection.close()
            print(error)
            sys.exit(1)  # Kill process and force uWSGI to Respawn

    keepalive()

    while True:
        for method_frame, _, body in channel.consume(queue_name):
            try:
                uwsgi.websocket_send(body)
            except OSError as error:
                print(error)
                sys.exit(1)  # Force uWSGI to Respawn
            else:
                # acknowledge the message
                channel.basic_ack(method_frame.delivery_tag)

回溯(最后一次调用):文件“websocket.py”,第 6 行,导入 uwsgi ModuleNotFoundError:没有名为“uwsgi”的模块

标签: pythondjangowebsocketuwsgiwsgi

解决方案


你如何开始你的代码?

如果你想导入 uwsgi,你可以(如果我没记错的话)启动 uwsgi 可执行文件,然后它会启动 uwsgi 工作程序。然后这些 uwsgi 工作者可以导入 uwsgi。


推荐阅读