summary refs log tree commit diff
path: root/synapse/app/_base.py
diff options
context:
space:
mode:
authorTravis Ralston <travpc@gmail.com>2018-10-12 14:49:58 -0600
committerTravis Ralston <travpc@gmail.com>2018-10-12 14:49:58 -0600
commite3586f7c06f3c779e87f9ea3bf4e4ac549a451c9 (patch)
tree2ef1ecdb2f2c6641a81a47adf51fda62343be7e3 /synapse/app/_base.py
parentRemove debugging statement (diff)
parentMake workers work on Py3 (#4027) (diff)
downloadsynapse-e3586f7c06f3c779e87f9ea3bf4e4ac549a451c9.tar.xz
Merge branch 'develop' into travis/fix-federated-group-requests
Diffstat (limited to 'synapse/app/_base.py')
-rw-r--r--synapse/app/_base.py30
1 files changed, 15 insertions, 15 deletions
diff --git a/synapse/app/_base.py b/synapse/app/_base.py
index 7c866e246a..18584226e9 100644
--- a/synapse/app/_base.py
+++ b/synapse/app/_base.py
@@ -17,6 +17,7 @@ import gc
 import logging
 import sys
 
+import psutil
 from daemonize import Daemonize
 
 from twisted.internet import error, reactor
@@ -24,12 +25,6 @@ from twisted.internet import error, reactor
 from synapse.util import PreserveLoggingContext
 from synapse.util.rlimit import change_resource_limit
 
-try:
-    import affinity
-except Exception:
-    affinity = None
-
-
 logger = logging.getLogger(__name__)
 
 
@@ -89,15 +84,20 @@ def start_reactor(
         with PreserveLoggingContext():
             logger.info("Running")
             if cpu_affinity is not None:
-                if not affinity:
-                    quit_with_error(
-                        "Missing package 'affinity' required for cpu_affinity\n"
-                        "option\n\n"
-                        "Install by running:\n\n"
-                        "   pip install affinity\n\n"
-                    )
-                logger.info("Setting CPU affinity to %s" % cpu_affinity)
-                affinity.set_process_affinity_mask(0, cpu_affinity)
+                # Turn the bitmask into bits, reverse it so we go from 0 up
+                mask_to_bits = bin(cpu_affinity)[2:][::-1]
+
+                cpus = []
+                cpu_num = 0
+
+                for i in mask_to_bits:
+                    if i == "1":
+                        cpus.append(cpu_num)
+                    cpu_num += 1
+
+                p = psutil.Process()
+                p.cpu_affinity(cpus)
+
             change_resource_limit(soft_file_limit)
             if gc_thresholds:
                 gc.set_threshold(*gc_thresholds)