Browse Source

多线程

yufeng0528 7 months ago
parent
commit
ab81afc5a2
5 changed files with 44 additions and 4 deletions
  1. 6 0
      draw/README
  2. 18 0
      draw/multi_thread.py
  3. 3 1
      draw/requirement.txt
  4. 12 0
      draw/uwsgi.ini
  5. 5 3
      draw/web_index.py

+ 6 - 0
draw/README

@@ -0,0 +1,6 @@
1
+/usr/local/python3/bin/uwsgi --ini uwsgi.ini             # 启动
2
+/usr/local/python3/bin/uwsgi --reload uwsgi.ini          # 重启
3
+/usr/local/python3/bin/uwsgi --stop uwsgi.ini            # 关闭
4
+
5
+使用 gevent
6
+uwsgi --gevent 100 --gevent-monkey-patch --ini uwsgi.ini

+ 18 - 0
draw/multi_thread.py

@@ -0,0 +1,18 @@
1
+import multiprocessing
2
+
3
+import tensorflow as tf
4
+
5
+def f(x):
6
+    session = tf.Session()
7
+    a = tf.Variable(x, name='a')
8
+    b = tf.Variable(100, name='b')
9
+    c = tf.multiply(a, b, name='c')
10
+    session.run(tf.global_variables_initializer())
11
+
12
+    out = session.run(c)
13
+    print("OK: %s" % out)
14
+
15
+if __name__ == '__main__':
16
+    multiprocessing.set_start_method('spawn')  # Comment me out to hang
17
+    f(0)
18
+    multiprocessing.Pool().map(f, range(10))

+ 3 - 1
draw/requirement.txt

@@ -2,4 +2,6 @@ opencv-python-headless
2 2
 tensorflow
3 3
 opencv-python
4 4
 mtcnn
5
-traceback2
5
+traceback2
6
+uwsgi
7
+gevent

+ 12 - 0
draw/uwsgi.ini

@@ -0,0 +1,12 @@
1
+[uwsgi]
2
+http = 0.0.0.0:5000
3
+chdir =/home/code/quantization-spider/bbztx/
4
+wsgi-file = /home/code/quantization-spider/bbztx/s1.py
5
+processes = 1
6
+threads = 8
7
+buffer-size = 32768
8
+master = false
9
+pidfile = uwsgi.pid
10
+daemonize = uwsgi.log
11
+callable = app
12
+cheaper = 0

+ 5 - 3
draw/web_index.py

@@ -33,6 +33,8 @@ _logger = logger(__name__)
33 33
 
34 34
 @app.route('/')
35 35
 def hello_world():
36
+    time.sleep(1)
37
+    print(int(round(time.time() * 1000)))
36 38
     return 'Hello World!'
37 39
 
38 40
 import requests
@@ -80,9 +82,9 @@ from gevent import pywsgi
80 82
 import traceback
81 83
 if __name__ == '__main__':
82 84
     try:
83
-        # app.run(host='192.168.0.220', port=5000)
84
-        server = pywsgi.WSGIServer(('0.0.0.0', 5000), app)
85
-        server.serve_forever()
85
+        app.run(host='192.168.0.220', port=5000)
86
+        # server = pywsgi.WSGIServer(('0.0.0.0', 5000), app)
87
+        # server.serve_forever(threaded=True)
86 88
     except Exception as e:
87 89
         traceback.print_exc(file=open('error.txt', 'a+'))
88 90
         print(e)