2017-01-28 01:34:21 +00:00
|
|
|
import multiprocessing
|
|
|
|
import random
|
|
|
|
import signal
|
2017-01-08 17:55:08 +00:00
|
|
|
|
2016-10-18 09:22:49 +01:00
|
|
|
from sanic import Sanic
|
2017-11-27 18:56:05 +00:00
|
|
|
from sanic.testing import HOST
|
2016-10-18 09:22:49 +01:00
|
|
|
|
|
|
|
|
2017-01-08 17:55:08 +00:00
|
|
|
def test_multiprocessing():
|
2017-01-28 01:34:21 +00:00
|
|
|
"""Tests that the number of children we produce is correct"""
|
|
|
|
# Selects a number at random so we can spot check
|
|
|
|
num_workers = random.choice(range(2, multiprocessing.cpu_count() * 2 + 1))
|
|
|
|
app = Sanic('test_multiprocessing')
|
|
|
|
process_list = set()
|
2016-12-26 22:37:05 +00:00
|
|
|
|
2017-01-28 01:34:21 +00:00
|
|
|
def stop_on_alarm(*args):
|
|
|
|
for process in multiprocessing.active_children():
|
|
|
|
process_list.add(process.pid)
|
|
|
|
process.terminate()
|
2016-12-26 22:37:05 +00:00
|
|
|
|
2017-01-28 01:34:21 +00:00
|
|
|
signal.signal(signal.SIGALRM, stop_on_alarm)
|
|
|
|
signal.alarm(1)
|
2017-11-27 18:56:05 +00:00
|
|
|
app.run(HOST, app.test_port, workers=num_workers)
|
2016-12-26 22:37:05 +00:00
|
|
|
|
2017-01-28 01:34:21 +00:00
|
|
|
assert len(process_list) == num_workers
|