【问题标题】:Request timeout/delay Issue请求超时/延迟问题
【发布时间】:2021-06-22 22:58:21
【问题描述】:

我的应用程序使用 Django REST 框架发布到 Instagram。发布到 Instagram 是一个两步过程。首先,您必须将内容发送到媒体容器。然后你必须等到 Instagram 处理完照片/视频。处理完媒体后,您将创建 ID 发送到 Instagram 以公开发布帖子。为了保证 Instagram 在发送创建 ID 之前有足够的时间处理媒体,我使用 Django 的 time.sleep 函数将请求暂停 60 秒。问题是这适用于我的桌面,但在我的 ec2 实例上,当我暂停请求超过 10 秒时,相同的代码会失败。为了运行我的应用程序,我使用了 amazon EC2、Gunicorn 和 nginx。

我的 Django 代码是这样的

```#print('posting photo to facebook')
    video_url = data.get('video_url')
    container_url = "https://graph.facebook.com/" + page_id + "/media?media_type=VIDEO&\
    video_url=" + video_url + "&caption=" + text + "&access_token=" + access_token
    res = requests.post(container_url)
    data = res.json()
    time.sleep(60)
    publish_url = "https://graph.facebook.com/" + page_id + "/media_publish?\
    creation_id=" + data['id'] + "&access_token=" + access_token
    res = requests.post(publish_url)
    data = res.json()
    print('result of video posting attempt')
    print(json.dumps(data));
    a = res.status_code
    b = 200
    #print(res.text)
    if a != b:
        return Response(data = {"data": {"id":"" , "post_id":"" }})
    return Response(data = data, status = res.status_code)```

我的 gunicorn.service 文件

       Description=gunicorn daemon
       Requires=gunicorn.socket
       After=network.target
      
       [Service]
       User=ubuntu
       Group=ubuntu
       WorkingDirectory=/home/ubuntu/xxxxx-xxxxxx-xxxxxx
       ExecStart=/home/ubuntu/xxxxx-xxxxxx-xxxxxx/xxxxxxx/bin/gunicorn \
       --access-logfile - \
       --workers 3 \
       --bind unix:/run/gunicorn.sock \
       xxxxxxxxxx.wsgi:application
       TimeoutStopSec=120

      [Install]
      WantedBy=multi-user.target

我的 nginx 配置文件

       listen 80;
       listen [::]:80;
       server_name xxxxxxxxx.com *.xxxxxxxxx.com;
       return 301 https://$host$request_uri;
       }

      server {
      listen 443 ssl http2;
      listen [::]:443 ssl http2;
      server_name xxxxxxxxx.com *.xxxxxxxxx.com;

      #SSL/TLS settings
      ssl_certificate /etc/letsencrypt/live/xxxxxxxxx.com/fullchain.pem;
      ssl_certificate_key /etc/letsencrypt/live/xxxxxxxxx.com/privkey.pem;
      client_max_body_size 100M;
      proxy_read_timeout 300;
      proxy_connect_timeout 300;
      proxy_send_timeout 300;

      location = /favicon.ico {
        access_log off;
        log_not_found off;
      }

      location /static/ {
        root /home/ubuntu/xxxxxxxxx-xxxxxxxxx-xxxxxxxxx;
      }

      location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
      }

      }```

【问题讨论】:

    标签: django nginx amazon-ec2 django-rest-framework gunicorn


    【解决方案1】:

    我建议使用celery 处理延迟任务。例如:

    @shared_task
    def task_make_public(publish_url):
        res = requests.post(publish_url)
        ...
    
    res = requests.post(container_url)
    data = res.json()
    publish_url = ...
    
    task_make_public.apply_async((publish_url,), eta=now + timedelta(seconds=60))
    

    更简单的设置适用于django-background-tasks。它不像 celery 那样需要 celeworker、celerybeat 和 reddis 来运行任务。

    从 PyPI 安装

    pip install django-background-tasks
    

    添加到 INSTALLED_APPS:

    INSTALLED_APPS = (
        # ...
        'background_task',
        # ...
    )
    

    迁移数据库

    python manage.py migrate
    

    代码示例

    from background_task import background
    from django.contrib.auth.models import User
    
    @background
    def task_make_public(publish_url):
        res = requests.post(publish_url)
        ...
    
    res = requests.post(container_url)
    data = res.json()
    publish_url = ...
    
    task_make_public(publish_url, schedule=timedelta(seconds=60)) # 60 seconds from now
    

    【讨论】:

    • 您好 Valery,感谢您的回复。
    猜你喜欢
    • 2019-06-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2019-12-15
    • 2017-03-22
    • 2013-09-05
    • 1970-01-01
    • 2013-01-08
    相关资源
    最近更新 更多