質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Gunicorn

Gunicorn (Green Unicorn)は、Rubyのunicornをベースに開発されたUNIX向けのPython製HTTPサーバです。他のライブラリとの依存関係がないため、容易にインストールして使用できます。

CSV

CSV(Comma-Separated Values)はコンマで区切られた明白なテキスト値のリストです。もしくは、そのフォーマットでひとつ以上のリストを含むファイルを指します。

Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

nginx

nginixは軽量で高性能なwebサーバーの1つです。BSD-likeライセンスのもとリリースされており、あわせてHTTPサーバ、リバースプロキシ、メールプロキシの機能も備えています。MacOSX、Windows、Linux、上で動作します。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Q&A

解決済

2回答

3152閲覧

1GバイトのCSVファイルがインポートしたい

kwmr8416

総合スコア6

Gunicorn

Gunicorn (Green Unicorn)は、Rubyのunicornをベースに開発されたUNIX向けのPython製HTTPサーバです。他のライブラリとの依存関係がないため、容易にインストールして使用できます。

CSV

CSV(Comma-Separated Values)はコンマで区切られた明白なテキスト値のリストです。もしくは、そのフォーマットでひとつ以上のリストを含むファイルを指します。

Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

nginx

nginixは軽量で高性能なwebサーバーの1つです。BSD-likeライセンスのもとリリースされており、あわせてHTTPサーバ、リバースプロキシ、メールプロキシの機能も備えています。MacOSX、Windows、Linux、上で動作します。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

0グッド

1クリップ

投稿2020/11/18 06:58

編集2020/11/18 07:22

前回の質問
https://teratail.com/questions/304864
で40MバイトのCSVファイルをインポートしようとしたところエラーになり、質問時点ではnginx.confの設定を行っていましたが、質問後gunicornの設定を追加で行うことで50MバイトのCSVファイルをインポートすることに成功したため自己解決としたところ、100MバイトのCSVファイルインポート時にエラーになってしまいました。表題の通り1Gバイトのファイルをインポートしたいため設定内容を見直しておりますが、よくわかりません。ブラウザに表示されるものに関しては「Server Error (500)」や「nginx error!」です。
以下に設定ファイルと100Mバイトのファイルのインポート時のログ情報を記述します。

nginx.con(client_max_body_sizeとして1Gを設定しました)

user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; # keepalive_timeout 65; keepalive_timeout 9999; types_hash_max_size 4096; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include proxy_connect_timeout 9999; proxy_send_timeout 9999; proxy_read_timeout 9999; send_timeout 9999; client_body_timeout 9999; client_header_timeout 9999; server { # 80番ポート(HTTP)でリクエストを待機する設定 listen 80; listen [::]:80; # ElasticIPアドレスを指定 server_name xxx.xxx.xxx.xxx; # 公開するディレクトリの設定 root /usr/share/nginx/html; client_max_body_size 1G; # 下記は必要な場合とない場合があるが記述しておく # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; # 静的ファイル配置ディレクトリとURLの対応付け location /static { alias /usr/share/nginx/html/static; } location / { # Host:ホスト名を転送 proxy_set_header Host $http_host; # X-Forwarded-For:送信元アドレスを転送 proxy_set_header X-Fowarded-For $proxy_add_x_forwarded_for; # X-Forwarded-Proto:URLスキーム(httpなど)を転送 proxy_set_header X-Fowrded-Proto $scheme; #client_max_body_size 1000M; client_max_body_size 1G; # URLが「/static」に該当しない場合(リバースプロキシ) proxy_pass http://127.0.0.1:8000; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } }

/var/log/nginx/error.log

(venv_xxx_project) [app_admin@ip-172-31-36-217 ~]$ sudo tail -f /var/log/nginx/error.log 2020/11/18 14:02:48 [error] 12701#0: *1 client intended to send too large body: 102416786 bytes, client: yyy.yyy.yyy.yyy, server: xxx.xxx.xxx.xxx, request: "POST /working_listworkinglistimport/ HTTP/1.1", upstream: "http://127.0.0.1:8000/working_listworkinglistimport/", host: "xxx.xxx.xxx.xxx", referrer: "http://xxx.xxx.xxx.xxx/working_listworkinglistimport/" 2020/11/18 14:04:56 [emerg] 12773#0: "client_body_buffer_size" directive invalid value in /etc/nginx/nginx.conf:61 2020/11/18 14:05:46 [emerg] 12787#0: "client_body_buffer_size" directive invalid value in /etc/nginx/nginx.conf:61 2020/11/18 14:05:52 [emerg] 12801#0: "client_body_buffer_size" directive invalid value in /etc/nginx/nginx.conf:61 2020/11/18 14:08:35 [error] 12827#0: *2 upstream prematurely closed connection while reading response header from upstream, client: yyy.yyy.yyy.yyy, server: xxx.xxx.xxx.xxx, request: "POST /working_listworkinglistimport/ HTTP/1.1", upstream: "http://127.0.0.1:8000/working_listworkinglistimport/", host: "xxx.xxx.xxx.xxx", referrer: "http://xxx.xxx.xxx.xxx/working_listworkinglistimport/" 2020/11/18 14:27:08 [emerg] 13007#0: *1 malloc(1024164151) failed (12: Cannot allocate memory), client: yyy.yyy.yyy.yyy, server: xxx.xxx.xxx.xxx, request: "POST /working_listworkinglistimport/ HTTP/1.1", host: "xxx.xxx.xxx.xxx", referrer: "http://xxx.xxx.xxx.xxx/working_listworkinglistimport/" 2020/11/18 14:27:38 [emerg] 13007#0: *3 malloc(1024164151) failed (12: Cannot allocate memory), client: yyy.yyy.yyy.yyy, server: xxx.xxx.xxx.xxx, request: "POST /working_listworkinglistimport/ HTTP/1.1", host: "xxx.xxx.xxx.xxx", referrer: "http://xxx.xxx.xxx.xxx/working_listworkinglistimport/" 2020/11/18 14:28:08 [emerg] 13007#0: *4 malloc(1024164151) failed (12: Cannot allocate memory), client: yyy.yyy.yyy.yyy, server: xxx.xxx.xxx.xxx, request: "POST /working_listworkinglistimport/ HTTP/1.1", host: "xxx.xxx.xxx.xxx", referrer: "http://xxx.xxx.xxx.xxx/working_listworkinglistimport/" 2020/11/18 14:32:53 [error] 13049#0: *1 sendfile() failed (104: Connection reset by peer) while sending request to upstream, client: yyy.yyy.yyy.yyy, server: xxx.xxx.xxx.xxx, request: "POST /working_listworkinglistimport/ HTTP/1.1", upstream: "http://127.0.0.1:8000/working_listworkinglistimport/", host: "xxx.xxx.xxx.xxx", referrer: "http://xxx.xxx.xxx.xxx/working_listworkinglistimport/" 2020/11/18 14:49:35 [error] 13406#0: *1 upstream prematurely closed connection while reading response header from upstream, client: yyy.yyy.yyy.yyy, server: xxx.xxx.xxx.xxx, request: "POST /working_listworkinglistimport/ HTTP/1.1", upstream: "http://127.0.0.1:8000/working_listworkinglistimport/", host: "xxx.xxx.xxx.xxx", referrer: "http://xxx.xxx.xxx.xxx/working_listworkinglistimport/" 2020/11/18 15:13:46 [error] 13553#0: *5 upstream prematurely closed connection while reading response header from upstream, client: yyy.yyy.yyy.yyy, server: xxx.xxx.xxx.xxx, request: "POST /working_listworkinglistimport/ HTTP/1.1", upstream: "http://127.0.0.1:8000/working_listworkinglistimport/", host: "xxx.xxx.xxx.xxx", referrer: "http://xxx.xxx.xxx.xxx/working_listworkinglistimport/"

django.log

(venv_xxx_project) [app_admin@ip-172-31-36-217 xxx_project]$ tail -f logs/django.log self.full_clean() File "/home/app_admin/venv_xxx_project/lib64/python3.7/site-packages/django/forms/forms.py", line 376, in full_clean self._clean_fields() File "/home/app_admin/venv_xxx_project/lib64/python3.7/site-packages/django/forms/forms.py", line 397, in _clean_fields value = getattr(self, 'clean_%s' % name)() File "/home/app_admin/venv_xxx_project/xxx_nippo_20201102/venv_xxx_project/xxx_project/working_list/forms.py", line 26, in clean_file for row in reader: MemoryError 2020-11-18 15:08:01,058 [WARNING] /home/app_admin/venv_xxx_project/lib64/python3.7/site-packages/django/utils/log.py(Line:228) Not Found: /.env 2020-11-18 15:08:03,157 [WARNING] /home/app_admin/venv_xxx_project/lib64/python3.7/site-packages/django/utils/log.py(Line:228) Not Found: / 2020-11-18 15:13:46,996 [WARNING] /home/app_admin/venv_xxx_project/lib64/python3.7/site-packages/django/utils/log.py(Line:228) Not Found: /nginx-logo.png 2020-11-18 15:13:46,998 [WARNING] /home/app_admin/venv_xxx_project/lib64/python3.7/site-packages/django/utils/log.py(Line:228) Not Found: /poweredby.png

環境
インスタンスタイプ:t2.micro
ap:django
web:nginx(gunicorn)
db:PostgreSQL

急ぎ対応に当たっており、尚早に自己解決としてしまったため2度手間になってしまいましたが、どんなことでも構いません、何か見落としている点があれば教えて下さい。よろしくお願いいたします。*文字数の関係で下記にgunicornのログを追記します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yu_1985

2020/11/19 02:02

そもそもの設計として、そんな巨大なファイルをWEBサーバにHTTPリクエストでアップロードさせようというのが割と無茶な気がしますが…。 それはともかく、tail -fはリアルタイムにログを確認するためのコマンドで、デフォルトだとログの一部しか見られません。 特にdjango.logは明らかに参照すべき箇所が欠けているのでcatなりlessなりでもうちょっと広い範囲を確認してください。
yu_1985

2020/11/19 02:03

> MemoryError django.logのこれがちょっと気になるところです。
kwmr8416

2020/11/19 02:34 編集

お世話になります。 本日の朝からcatでログを再度確認していましたが、有用な情報は得られませんでした。  現状までにDBの設定ファイル(/var/lib/pgsql/data/postgresql.conf)の共有メモリバッファ(shared_buffers)をデフォルトの128MBから256MBに変更し、再度実行しましたが、前述のエラーは解消されませんでした。 django.log MemoryErrorは確かに気になり、昨夜freeコマンドでメモリの使用状況を確認してみました。 その時は初めて利用したコマンドですぐにはわからなかったんですが、 確認済みインポート可能最大サイズの70MBのファイルをインポートし最後インポートし終わる頃のfreeコマンドの出力($free -m -s 3)に着目すると、 ※この欄はヘッダーが左詰めに表示されてしまうため下の回答欄に追記ましたので、そちらをご覧ください。 ``` total used free shared buff/cache available Mem: 983 774 65 109 143 12 Swap: 0 0 0 total used free shared buff/cache available Mem: 983 774 63 110 145 11 Swap: 0 0 0 total used free shared buff/cache available Mem: 983 774 71 110 137 15 Swap: 0 0 0 total used free shared buff/cache available Mem: 983 160 665 110 156 619 Swap: 0 0 0 total used free shared buff/cache available Mem: 983 160 666 110 156 619 Swap: 0 0 0 total used free shared buff/cache available Mem: 983 160 665 110 157 619 Swap: 0 0 0 total used free shared buff/cache available Mem: 983 160 664 110 158 618 Swap: 0 0 0 ``` 後半のusedのメモリが減りfreeのメモリが増えているのはインポートが終わった直後の処理をしていない状態です。 メモリ不足でしょうか。
kwmr8416

2020/11/19 03:12 編集

ただいまt2.microからt2.smallにインスタンスを変更(メモリ1GBから2GB)してインポートを実行しています。 エラーが出ていた80MBのファイルをインポートしていますが、usedが850MBほどになっています。 これがエラー原因みたいです。 追記 ただいま80MBのファイルがインポート成功しました。 もうすこしファイルサイズやインスタンスタイプを試してみます。
guest

回答2

0

ベストアンサー

どうやらメモリ不足で落ちている、でほぼ間違いなさそうですね。

コメントにも書いたとおり、そもそも1Gのファイル受信をHTTPリクエスト経由でWEBサーバに行わせる、という設計が割と無茶な気がするので、S3にファイルを置かせたり、なにかこのcsvを元に処理を行うなら別サーバに処理を行わせたり等設計をどうにかしたほうがいいと思います。
そこだけがネックになってスペック上げるのはちょっと違うと思いますし

投稿2020/11/19 03:49

編集2020/11/19 04:00
yu_1985

総合スコア7440

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kwmr8416

2020/11/20 00:34

ご回答ありがとうございます。 「1GBのCSVファイルをインポートする」という設計案についてご指摘頂きましたが、今回は学習目的もかねて取り組まさせていただきました。 ソースコードについてもマルチスレッドの対応が必要になるかと思いますが、 インスタンスはt2.xlarge(メモリ16GB)に変更し、1GBのCSVファイルの読み込みに成功しました。 ただ想定したインポート終了時間にサーバーダウンし、「The page you are looking for is temporarily unavailable. Please try again later.」というNginxのロゴ画面が表示されてしまったんですが、しばらくすると通常通りアプリケーションにアクセスでき、インポート後のデータが表示されることを確認しました。まだ色々と問題はありますが、以上で今回の件は終了とさせていただきます。ありがとうございました。
guest

0

上記質問欄に文字数が足りなくなったため追記します。

gunicorn:log(timeoutに9999を設定しました)

(venv_xxx_project) [app_admin@ip-172-31-36-217 xxx_project]$ gunicorn --bind 127.0.0.1:8000 xxx_project.wsgi --log-level=DEBUG --timeout 9999 [2020-11-18 15:12:29 +0900] [13563] [DEBUG] Current configuration: config: None bind: ['127.0.0.1:8000'] backlog: 2048 workers: 1 worker_class: sync threads: 1 worker_connections: 1000 max_requests: 0 max_requests_jitter: 0 timeout: 9999 graceful_timeout: 30 keepalive: 2 limit_request_line: 4094 limit_request_fields: 100 limit_request_field_size: 8190 reload: False reload_engine: auto reload_extra_files: [] spew: False check_config: False preload_app: False sendfile: None reuse_port: False chdir: /home/app_admin/venv_xxx_project/xxx_nippo_20201102/venv_xxx_project/xxx_project daemon: False raw_env: [] pidfile: None worker_tmp_dir: None user: 1001 group: 1001 umask: 0 initgroups: False tmp_upload_dir: None secure_scheme_headers: {'X-FORWARDED-PROTOCOL': 'ssl', 'X-FORWARDED-PROTO': 'https', 'X-FORWARDED-SSL': 'on'} forwarded_allow_ips: ['127.0.0.1'] accesslog: None disable_redirect_access_to_syslog: False access_log_format: %(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s" errorlog: - loglevel: DEBUG capture_output: False logger_class: gunicorn.glogging.Logger logconfig: None logconfig_dict: {} syslog_addr: udp://localhost:514 syslog: False syslog_prefix: None syslog_facility: user enable_stdio_inheritance: False statsd_host: None dogstatsd_tags: statsd_prefix: proc_name: None default_proc_name: xxx_project.wsgi pythonpath: None paste: None on_starting: <function OnStarting.on_starting at 0x7f2d588e0d40> on_reload: <function OnReload.on_reload at 0x7f2d588e0e60> when_ready: <function WhenReady.when_ready at 0x7f2d588e0f80> pre_fork: <function Prefork.pre_fork at 0x7f2d588f30e0> post_fork: <function Postfork.post_fork at 0x7f2d588f3200> post_worker_init: <function PostWorkerInit.post_worker_init at 0x7f2d588f3320> worker_int: <function WorkerInt.worker_int at 0x7f2d588f3440> worker_abort: <function WorkerAbort.worker_abort at 0x7f2d588f3560> pre_exec: <function PreExec.pre_exec at 0x7f2d588f3680> pre_request: <function PreRequest.pre_request at 0x7f2d588f37a0> post_request: <function PostRequest.post_request at 0x7f2d588f3830> child_exit: <function ChildExit.child_exit at 0x7f2d588f3950> worker_exit: <function WorkerExit.worker_exit at 0x7f2d588f3a70> nworkers_changed: <function NumWorkersChanged.nworkers_changed at 0x7f2d588f3b90> on_exit: <function OnExit.on_exit at 0x7f2d588f3cb0> proxy_protocol: False proxy_allow_ips: ['127.0.0.1'] keyfile: None certfile: None ssl_version: 2 cert_reqs: 0 ca_certs: None suppress_ragged_eofs: True do_handshake_on_connect: False ciphers: None raw_paste_global_conf: [] strip_header_spaces: False [2020-11-18 15:12:29 +0900] [13563] [INFO] Starting gunicorn 20.0.4 [2020-11-18 15:12:29 +0900] [13563] [DEBUG] Arbiter booted [2020-11-18 15:12:29 +0900] [13563] [INFO] Listening at: http://127.0.0.1:8000 (13563) [2020-11-18 15:12:29 +0900] [13563] [INFO] Using worker: sync [2020-11-18 15:12:29 +0900] [13566] [INFO] Booting worker with pid: 13566 [2020-11-18 15:12:29 +0900] [13563] [DEBUG] 1 workers [2020-11-18 15:13:00 +0900] [13566] [DEBUG] GET /working_list [2020-11-18 15:13:02 +0900] [13566] [DEBUG] GET /working_listworkinglistimport/ [2020-11-18 15:13:31 +0900] [13566] [DEBUG] POST /working_listworkinglistimport/ [2020-11-18 15:13:46 +0900] [13573] [INFO] Booting worker with pid: 13573 [2020-11-18 15:13:46 +0900] [13573] [DEBUG] GET /nginx-logo.png [2020-11-18 15:13:46 +0900] [13573] [DEBUG] GET /poweredby.png

追記11-29-11:29
質問の回答でfreeコマンドの結果を貼り付けたらヘッダー情報が左詰めになってしまうため誤解がないようにここに貼り付けます。

total used free shared buff/cache available Mem: 983 774 64 108 144 12 Swap: 0 0 0 total used free shared buff/cache available Mem: 983 773 72 108 137 16 Swap: 0 0 0 total used free shared buff/cache available Mem: 983 773 70 109 139 15 Swap: 0 0 0 total used free shared buff/cache available Mem: 983 774 67 109 141 14 Swap: 0 0 0 total used free shared buff/cache available Mem: 983 774 65 109 143 12 Swap: 0 0 0 total used free shared buff/cache available Mem: 983 774 63 110 145 11 Swap: 0 0 0 total used free shared buff/cache available Mem: 983 774 71 110 137 15 Swap: 0 0 0 total used free shared buff/cache available Mem: 983 160 665 110 156 619 Swap: 0 0 0 total used free shared buff/cache available Mem: 983 160 666 110 156 619 Swap: 0 0 0 total used free shared buff/cache available Mem: 983 160 665 110 157 619 Swap: 0 0 0 total used free shared buff/cache available Mem: 983 160 664 110 158 618 Swap: 0 0 0 total used free shared buff/cache available Mem: 983 160 663 110 159 618 Swap: 0 0 0 total used free shared buff/cache available Mem: 983 160 656 110 165 614 Swap: 0 0 0

投稿2020/11/18 06:59

編集2020/11/19 02:32
kwmr8416

総合スコア6

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問