Home  >  Article  >  Backend Development  >  python+tornado+supervisor+nginx deployment under ubuntu

python+tornado+supervisor+nginx deployment under ubuntu

PHP中文网
PHP中文网Original
2017-07-14 18:13:091218browse

Since the data previously collected in the hospital were prescription pictures obtained by taking photos, what needs to be used is the text form of the prescription. Therefore, I wrote a small program in the past two weeks to display pictures of the server to users (hiring some people at that time), allowing users to enter text information based on the pictures.

I used Java to write web pages before. I thought that I would use Python to learn machine learning recently, so I used Python to write it. In addition, because I wanted to use something new and the program was relatively small, I considered using mongodb for storage. (Although it's really not necessary).

The basic architecture is like this: (backend language) python + (web framework and web server) tornado + (database) mongodb + (process management) supervisor + (reverse proxy) nginx

(1) python: I am watching Liao Xuefeng’s python tutorial

I learned some basic sentences, and then I checked them if I didn’t understand them when I was doing them.

(2) tornado: I started learning by watching this tutorial

tornado is very fast to use. It can map requests directly like a forwarder in spring, and directly monitor http requests on a certain port like tomcat. Tornado has done all these things for us, we only need to write The get and post methods are fine.

The rest is almost the same as in java web. Without the framework of springMVC and hibernate, you can layer controller, service, and dao in the python package yourself.

(3) mongodb: Non-relational database, there is no requirement for the same field name to be unified for each row of data (called collection in mongodb) in a table (called collection in mongodb). More or less, uneven. Another point is that transactions are not supported, and there is no way to roll back when executing multiple read and write statements in Python, so you have to find a way to prevent problems caused by concurrency in the program.

For example, I need to find a document with a status of 0 from the collection and update it to 1, which means the user is viewing it. After the user makes modifications, then set the status to 2 and then put it back. Here 0 means not browsed, 1 It means it is being browsed, and 2 means it has been modified. At this time, many users may have obtained the data with status 0 at the same time. This goes against our wishes and allows many people to modify the same data. You can consider directly update_one to change the status of a document from 0 to 1, and then add a unique field to identify the document, and use this unique field to find the document. At this time, other users cannot obtain the document because update_one It will be locked when the time comes, and others cannot get this data.

(4) supervisor: Supervisor is a process management program developed in python. It can turn ordinary commands into background daemons and monitor the process status.

After installation, you can add the configuration file printprescription.conf in the /etc/supervisor/conf.d directory. The configuration example is as follows

[program:printprescription] #设置守护进程名
command = python /home/liaohuqiang/Code/printprescription/main2.py #设置执行命令
autorstart=true #设置随supervisor的启动而启动
stdout_logfile = /home/liaohuqiang/Code/printprescription/printprescription.log #设置日志路径

After the configuration is completed, you can open, close and restart the supervisor service. You can use supervisorctl status to check the process status, and you can also check the error information in the relevant log files

sudo /etc/init.d/supervisor start
sudo /etc/init.d/supervisor stop
sudo /etc/init.d/supervisor restart<br><br>sudo supervisorctl status printprescripton<br>vim cat /var/log/supervisor/xxx.log

 

(5) nginx: nginx is used as a reverse proxy. It can monitor port 80 and forward corresponding requests to other hosts and other ports. The path of the image we display here is not in tornado in static (static is used to store front-end static resources such as js and css), but in another location on the server, so the image can be obtained through nginx forwarding. On the other hand, other ports (such as the 8001 port that tornado listens on) are generally not exposed to users, so nginx is used for forwarding. In addition, the location /static/ directive tells nginx to provide static directory files directly instead of proxying requests to tornado. nginx can provide static files more efficiently than tornado.

After installing nginx, check the relevant configuration files and find this sentence: include /etc/nginx/sites-enabled/*. You can create a new configuration file in this directory for configuration

cat /etc/nginx/nginx.conf<br>sudo touch /etc/nginx/sites-enabled/default2

The configuration example is as follows. It should be noted that the bottom http://127.0.0.1:8001/ If the last slash here is not typed, the above printprescription will actually be added. When the user accesses "host/printprescription" It is equivalent to accessing "Host:8001/printprescription". Since the request on Tornado is mapped to Host:8001/, 404 will appear at this time.

<span style="color: #000000">  upstream frontends {
      server </span><span style="color: #800080">222.222</span>.<span style="color: #800080">222.222</span>:<span style="color: #800080">8000</span><span style="color: #000000">;
      server </span><span style="color: #800080">222.222</span>.<span style="color: #800080">222.222</span>:<span style="color: #800080">8001</span><span style="color: #000000">;
  }
  
  server {
      listen </span><span style="color: #800080">80</span><span style="color: #000000">;
  
      location </span>/<span style="color: #000000"> {
         proxy_read_timeout </span><span style="color: #800080">1800</span><span style="color: #000000">;
         proxy_pass_header Server;
         proxy_set_header Host $http_host;
         proxy_redirect off;
         proxy_set_header X</span>-Real-<span style="color: #000000">IP $remote_addr;
         proxy_set_header X</span>-<span style="color: #000000">Scheme $scheme;
         proxy_pass http:</span><span style="color: #008000">//</span><span style="color: #008000">frontends;</span>
<span style="color: #000000">     }
 
    location </span>/doctorAfter/<span style="color: #000000"> {  #配置图片路径
         root </span>/home/<span style="color: #000000">wenserver;
    }
 
    location </span>/static/<span style="color: #000000"> {  #配置前端静态资源路径
         root </span>/home/xxx/Code/printprescription/<span style="color: #000000">view;
    }
 
    location </span>/printprescription/<span style="color: #000000"> {
         proxy_read_timeout </span><span style="color: #800080">1800</span><span style="color: #000000">;
         proxy_pass_header Server;
         proxy_set_header Host $http_host;
         proxy_redirect off;
         proxy_set_header X</span>-Real-<span style="color: #000000">IP $remote_addr;
         proxy_set_header X</span>-<span style="color: #000000">Scheme $scheme;
         proxy_pass http:</span><span style="color: #008000">//</span><span style="color: #008000">127.0.0.1:8001/;</span>
<span style="color: #000000">     }
 
 }</span>

 

(6)opencv:一个计算机视觉库,实现了图像处理和计算机视觉方面的很多通用算法。由于之前采集数据的时候在处方图片数据中混杂了人脸图片,所以采用oepncv里的算法检测人脸,如果是人脸则删除该记录。然而还是会残留一些人脸图片,需要人工清洗,这个数据好像之前有师兄清洗过,到时候拿过来重构一下数据库。

ps:安装opencv的时候imshow报错,虽然imshow用不到,但是强迫症使然,搞了我一天,来来回回装了十多次,最后还是没搞定,真是心力交瘁,特想吐嘈:垃圾opencv!毁我青春!颓我精神!耗我时间!磨我意志!浪费人生!!!

好吧我就随便说说,回头还要靠它干活。

cv2.error: /io/opencv/modules/highgui/src/window.<span style="color: #0000ff">cpp</span>:<span style="color: #800080">583</span>: error: (-<span style="color: #800080">2</span>) The <span style="color: #0000ff">function</span> is not implemented. Rebuild the library with Windows, GTK+ <span style="color: #800080">2</span>.x or Carbon support. If you are on Ubuntu or Debian, <span style="color: #0000ff">install</span> libgtk2.<span style="color: #800080">0</span>-dev and <span style="color: #0000ff">pkg-config</span>, <span style="color: #0000ff">then</span> re-run cmake or configure script <span style="color: #0000ff">in</span> <span style="color: #0000ff">function</span> cvShowImage

 

The above is the detailed content of python+tornado+supervisor+nginx deployment under ubuntu. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn