【问题标题】:Slow Socket IO response when using Docker使用 Docker 时 Socket IO 响应慢
【发布时间】:2021-04-25 07:24:54
【问题描述】:

我有一个内置在 Flask 中的 Web 应用程序,其中捕获推文(使用 Tweepy 库)并显示在前端。我使用 Socket IO 在前端实时显示推文。

当我在本地运行时,我的代码运行良好。推文会立即出现。

但是,当我 Docker 化 Web 应用程序时,前端不会立即更新。显示更改需要一些时间(有时我认为推文由于速度缓慢而丢失)

以下是我网站的代码摘录:

fortsocket.js

$(document).ready(function () {



/************************************/
  /*********** My Functions ***********/
  /************************************/
  function stream_active_setup() {
    $("#favicon").attr("href", "/static/icons/fortnite-active.png");
    $("#stream-status-ic").attr("src", "/static/icons/stream-active.png");
    $("#stream-status-text").text("Live stream active");
  }

  function stream_inactive_setup() {
    $("#favicon").attr("href", "/static/icons/fortnite-inactive.png");
    $("#stream-status-ic").attr("src", "/static/icons/stream-inactive.png");
    $("#stream-status-text").text("Live stream inactive");
  }



  /*********************************/
  /*********** My Events ***********/
  /*********************************/

  // Socket connection to server

  // Prometheus
  //var socket = io.connect('http://104.131.173.145:8083');

  // Local
  var socket = io.connect(window.location.protocol + '//' + document.domain + ':' + location.port);

  // Heroku
  //var socket = io.connect('https://fortweet.herokuapp.com/');

  // Send a hello to know
  // if a stream is already active
  socket.on('connect', () => {
    socket.emit('hello-stream', 'hello-stream');
  });

  // Listene for reply from hello
  socket.on('hello-reply', function (bool) {
    if (bool == true) {
      stream_active_setup()
    } else {
      stream_inactive_setup()
    }
  });

  // Listens for tweets
  socket.on('stream-results', function (results) {

    // Insert tweets in divs
    $('#live-tweet-container').prepend(`
    <div class="row justify-content-md-center mt-3">
      <div class="col-md-2">
          <img width="56px" height="56px"  src="${results.profile_pic !== "" ? results.profile_pic : "/static/icons/profile-pic.png"}" class="mx-auto d-block rounded"  alt="">
      </div>
      <div class="col-md-8 my-auto">
        <div><b>${results.author}</b></div>
        <div>${results.message}</div>
      </div>
    </div>
    `);
  });

  // Listener for when a stream of tweets starts
  socket.on('stream-started', function (bool) {
    if (bool == true) {
      stream_active_setup()
    }
  });

  // Listener for when a stream of tweets ends
  socket.on('stream-ended', function (bool) {
    if (bool == true) {
      stream_inactive_setup()
    }
  });

});

init.py

# Create the app
app = create_app()

# JWT Configurations
jwt = JWTManager(app)

# Socket IO
socketio = SocketIO(app, cors_allowed_origins="*")

# CORS
CORS(app)
app.config["CORS_HEADERS"] = "Content-Type"

# Creates default admins and insert in db
create_default_admin()

# Main error handlers
@app.errorhandler(404)  # Handling HTTP 404 NOT FOUND
def page_not_found(e):
    return Err.ERROR_NOT_FOUND


# Listen for hello emit data
# from client
@socketio.on("hello-stream")
def is_stream_active(hello_stream):
    emit("hello-reply", streamer.StreamerInit.is_stream_active(), broadcast=True)

streamer.py

import time
import tweepy
import threading as Coroutine
import app.messages.constants as Const
import app.setup.settings as settings_mod
import app.models.tweet as tweet_mod
import app.services.logger as logger
import app


class FStreamListener(tweepy.StreamListener):
    def __init__(self):
        self.start_time = time.time()
        self.limit = settings_mod.TwitterSettings.get_instance().stream_time

        logger.get_logger().debug("Live capture has started")

        # Notify client that a live capture will start
        app.socketio.emit(
            "stream-started", True, broadcast=True,
        )

        super(FStreamListener, self).__init__()

    def on_status(self, status):
        if (time.time() - self.start_time) < self.limit:

            # Create tweet object
            forttweet = tweet_mod.TweetModel(
                status.source,
                status.user.name,
                status.user.profile_background_image_url_https,
                status.text,
                status.created_at,
                status.user.location,
            )

            # Emit to socket
            app.socketio.emit(
                "stream-results",
                {
                    "profile_pic": forttweet.profile_pic,
                    "author": forttweet.author,
                    "message": forttweet.message,
                },
                broadcast=True,
            )

            # Add to database
            forttweet.insert()

            return True
        else:
            logger.get_logger().debug("Live capture has ended")

            # Notify client that a live capture has ended
            app.socketio.emit(
                "stream-ended", True, broadcast=True,
            )

            # Stop the loop of streaming
            return False

    def on_error(self, status):
        logger.get_logger().debug(f"An error occurred while fetching tweets: {status}")
        raise Exception(f"An error occurred while fetching tweets: {status}")


class StreamerInit:

# [Private] Twitter configurations
def __twitterInstantiation(self):
    # Get settings instance
    settings = settings_mod.TwitterSettings.get_instance()
    # Auths
    auth = tweepy.OAuthHandler(settings.consumer_key, settings.consumer_secret,)
    auth.set_access_token(
        settings.access_token, settings.access_token_secret,
    )
    # Get API
    api = tweepy.API(auth)
    # Live Tweets Streaming
    myStreamListener = FStreamListener()
    myStream = tweepy.Stream(auth=api.auth, listener=myStreamListener)
    myStream.filter(track=settings.filters)

def start(self):
    for coro in Coroutine.enumerate():
        if coro.name == Const.FLAG_TWEETS_LIVE_CAPTURE:
            return False

    stream = Coroutine.Thread(target=self.__twitterInstantiation)
    stream.setName(Const.FLAG_TWEETS_LIVE_CAPTURE)
    stream.start()

    return True

@staticmethod
def is_stream_active():
    for coro in Coroutine.enumerate():
        if coro.name == Const.FLAG_TWEETS_LIVE_CAPTURE:
            return True

    return False

点击按钮调用 streamer.py

Dockerfile

# Using python 3.7 in Alpine
FROM python:3.6.5-stretch

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

RUN apt-get update -y && apt-get upgrade -y && pip install -r requirements.txt

# Run the command
ENTRYPOINT ["uwsgi", "app.ini"]

#ENTRYPOINT ["./entry.sh"]

docker-compose.yml

version: "3.8"

services:
  fortweet:
    container_name: fortweet
    image: mervin16/fortweet:dev
    build: ./
    env_file:
      - secret.env
    networks:
      plutusnet:
        ipv4_address: 172.16.0.10
    expose:
      - 8083
    restart: always

  nginx_fortweet:
    image: nginx
    container_name: nginx_fortweet
    ports:
      - "8083:80"
    networks:
      plutusnet:
        ipv4_address: 172.16.0.100
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - fortweet
    restart: always

networks:
  plutusnet:
    name: plutus_network
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 172.16.0.0/24
          gateway: 172.16.0.1

app.ini

[uwsgi]

module = run:app

master = true
processes = 5

# Local & Prometheus
http-socket = 0.0.0.0:8083

http-websockets = true

chmod-socket = 660
vacuum = true

die-on-term = true

如需完整的更新代码,您可以在dev/mervin 分支下找到here

感谢任何帮助。

【问题讨论】:

  • 你在本地测试时是否也在使用 nginx 服务器?能提供一下配置吗?你能用 docker 测试你的应用程序,但没有 nginx 作为代理吗?
  • 正如@Jan 所指出的,如果你在本地测试时没有使用 nginx,那么这两种情况是不等价的,你应该首先测试一个更简单的 hello world 类型的 web 应用程序来检查任何问题跨度>
  • @Jan 和 Piyush 我尝试删除 nginx 部分,但问题仍然存在。推文迟到了。我怀疑这与错误的 UWSGI 配置有关。我在 UWSGI 部分做错了什么吗?
  • 顺便问一下,你为什么在bridge而不是host下运行你的docker网络?您是否有其他容器需要它们连接到同一个桥接网络?如果没有,则删除所有 ip 引用,将其更改为 host,它直接绑定到您的 docker 主机的网络。

标签: python docker flask socket.io flask-socketio


【解决方案1】:

为了看看 ipv6 是否负责,我建议你关闭所有东西

打开 /etc/sysctl.conf 并添加以下行以禁用 ipv6

net.ipv6.conf.all.disable_ipv6=1
net.ipv6.conf.default.disable_ipv6=1
net.ipv6.conf.lo.disable_ipv6=1

运行 sudo sysctl -p 以便更改生效

再次启动 nginx 和 docker

如果您没有发现任何差异,则只需将设置更改为 0 并重新运行 sysctl -p 并告诉我

【讨论】:

    【解决方案2】:

    很遗憾,我无法在没有配置的情况下重现该问题,因此无法验证我的答案。

    我在 JP 的博客上找到了类似的问题:Performance problems with Flask and Docker

    简而言之,可能是容器上同时具有 IPv6 和 IPv4 配置导致了问题。 为了验证问题:

    1. 运行泊坞窗
    2. 进入正在运行的容器并更改主机文件,使其不会将 IPv6 映射到 localhost
    3. 在容器内再次运行应用程序

    如果应用运行顺畅,那么您已经确定了您的问题。 解决方案是调整 uwsgi 参数。

    作者在博文中做了什么:

    CMD uwsgi -s /tmp/uwsgi.sock -w project:app --chown-socket=www-data:www-data --enable-threads &amp; nginx -g 'daemon off;'

    【讨论】:

      猜你喜欢
      • 2020-10-10
      • 2018-08-07
      • 1970-01-01
      • 1970-01-01
      • 2019-04-20
      • 2021-11-22
      • 1970-01-01
      • 2018-03-19
      • 2021-12-04
      相关资源
      最近更新 更多