{"id":6382,"date":"2018-01-23T18:48:25","date_gmt":"2018-01-23T10:48:25","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=6382"},"modified":"2018-01-23T18:48:25","modified_gmt":"2018-01-23T10:48:25","slug":"python%e7%a8%8b%e5%ba%8f%e7%9a%84stdout%e5%b9%b6%e6%9c%aa%e8%be%93%e5%87%ba%e5%88%b0supervisor%e7%9a%84log%e4%b8%ad","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/6382.html","title":{"rendered":"python\u7a0b\u5e8f\u7684stdout\u5e76\u672a\u8f93\u51fa\u5230supervisor\u7684log\u4e2d"},"content":{"rendered":"<p>\u7528supervisor\u7ba1\u7406\u8fd0\u884c\u7684python\u7a0b\u5e8f\uff0c\u5c06stdout\u914d\u7f6e\u5230\u6307\u5b9alog\u6587\u4ef6\uff0csupervisor\u914d\u7f6e\u8282\u70b9\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;program:web]\r\ncommand = \/var\/www\/env\/bin\/python run.py --port=808%(process_num)01d --address=127.0.0.1\r\nautostart = true\r\nstartsecs = 5\r\nuser = root\r\nredirect_stderr = true\r\ndirectory = \/var\/www\/\r\nnumprocs=4\r\nprocess_name=%(program_name)s_%(process_num)02d\r\nstdout_logfile = \/var\/log\/supervisor\/www.log\r\n<\/pre>\n<p>\u7ed3\u679c\u53d1\u73b0supervisor\u8f93\u51fa\u7684\u65e5\u5fd7\u6587\u4ef6 www.log \u8ddf\u7a0b\u5e8f\u8f93\u51fa\u7684\u4e0d\u4e00\u81f4\uff0c\u5c11\u4e86\u5f88\u591a\u65e5\u5fd7\u8f93\u51fa\u3002<\/p>\n<p>\u6bd4\u5982\u540c\u4e00\u79d2\u5e76\u53d1\u4e865\u4e2a\u8bf7\u6c42\uff0cwww.log\u5374\u53ea\u6709\u4e00\u884c\u65e5\u5fd7\uff0c\u8fd9\u6837\u6211\u914d\u7f6e\u597d\u7684fail2ban\u901a\u8fc7www.log\u65e5\u5fd7\u6587\u4ef6\u7684\u5339\u914d\u89c4\u5219\u6765\u5b9a\u4f4d\uff0c\u5f53\u7136\u5c31\u4e0d\u80fd\u6b63\u786e\u5730\u8ba1\u7b97\u51fa\u8bbf\u95ee\u6570\u91cf\u3002<\/p>\n<p>\u6709\u60f3\u5230\u662fstdout\u7684buffer\u5bfc\u81f4\u7684\uff0c\u5728\u7f51\u4e0agoogle\u540e\u627e\u5230\u4e86\u7b54\u6848\uff1a<\/p>\n<p>If you are also using Supervisor to monitor and heal your long running Python projects and observed that output of your program is not being logged to stdout_logfile, it is because Python print statement does not automatically flush output to STDOUT.<\/p>\n<p>One solution is using sys.stdout.flush() frequently to flush the output or if you are using Python 3.3, print(msg, flush=True) is another solution. However, a better solution is to run python with -u parameter (unbuffered mode).<\/p>\n<p>\u8f83\u597d\u7684\u89e3\u51b3\u529e\u6cd5\u662f\u4fee\u6539supervisor\u914d\u7f6e\uff0c\u5728\u542f\u52a8python\u8fdb\u7a0b\u7684cmd\u4e2d\u6dfb\u52a0-u\u53c2\u6570\uff0c\u5982\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;program:web]\r\ncommand = \/var\/www\/env\/bin\/python run.py -u --port=808%(process_num)01d --address=127.0.0.1\r\n<\/pre>\n<p>\u53c2\u8003\uff1a<\/p>\n<p>https:\/\/stackoverflow.com\/questions\/15453234\/why-does-stdout-not-flush-when-connecting-to-a-process-that-is-run-with-supervis<\/p>\n<p>https:\/\/ahmet.im\/blog\/redirecting-output-of-python-programs-supervisor\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7528supervisor\u7ba1\u7406\u8fd0\u884c\u7684python\u7a0b\u5e8f\uff0c\u5c06stdout\u914d\u7f6e\u5230\u6307\u5b9alog\u6587\u4ef6\uff0csupervisor\u914d\u7f6e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-6382","post","type-post","status-publish","format-standard","hentry","category-skill"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6382","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=6382"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6382\/revisions"}],"predecessor-version":[{"id":6383,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6382\/revisions\/6383"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=6382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=6382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=6382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}