Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
175 views
in Technique[技术] by (71.8m points)

python - Use of multiprocess in a Bottle app create multiple bottle instance

I have a Bottle web application running a long task, to win some time i've used multiprocessing Pool. Outside the webapp (so only running the code in my IDE) it seems good, from 54 sec to 14. But if I run my multiprocessed function in my webapp context, it only show me X times (X correspond to my Pool size) :

Bottle v0.12.18 server starting up (using WSGIRefServer())...

Example

main.py

from bottle import route, run
from functions import runFunction


@route('/')
def index():
    runFunction()
    return "<b>Hello</b>!"

run(host='localhost', port=8083,debug=False, reloader=True)

functions.py

import multiprocessing as mp
import time
import random

def longTask(x):
    print(mp.current_process())
    y = random.randint(1,5)
    print(y)
    time.sleep(y)
    return x**x

def runFunction():
    start_time = time.time()
    pool = mp.Pool(3)
    result = pool.map(longTask, [4, 2, 3, 5, 3, 2, 1, 2])
    print(result)
    
if __name__ == '__main__':
    runFunction()

If you run only main in functions.py, output is :

<SpawnProcess(SpawnPoolWorker-1, started daemon)>
1
<SpawnProcess(SpawnPoolWorker-3, started daemon)>
4
<SpawnProcess(SpawnPoolWorker-2, started daemon)>
2
<SpawnProcess(SpawnPoolWorker-1, started daemon)>
1
<SpawnProcess(SpawnPoolWorker-1, started daemon)>
2
<SpawnProcess(SpawnPoolWorker-2, started daemon)>
1
<SpawnProcess(SpawnPoolWorker-2, started daemon)>
1
<SpawnProcess(SpawnPoolWorker-1, started daemon)>
2
[256, 4, 27, 3125, 27, 4, 1, 4]

Process finished with exit code 0

If you start your bottle app and go on http://localhost:8083/ it will output :

Bottle v0.12.18 server starting up (using WSGIRefServer())...
Listening on http://localhost:8083/
Hit Ctrl-C to quit.

Bottle v0.12.18 server starting up (using WSGIRefServer())...
Listening on http://localhost:8083/
Hit Ctrl-C to quit.

Bottle v0.12.18 server starting up (using WSGIRefServer())...
Listening on http://localhost:8083/
Hit Ctrl-C to quit.

I've certainly missed something in the multiprocess logic but I cannot find what. Any idea on what's going on ?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The main.py file is imported by multiprocessing during process startup and all new processes spawn new bottle servers by calling run(host=.... Just make sure that run is not executed when the file is imported:

if __name__ == '__main__':
    run(host='localhost', port=8083,debug=False, reloader=True)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...