We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
make sage.parallel.ncpus.ncpus() use os.cpu_count()
<div id="comment:0"></div>

Currently, `sage.parallel.ncpus.ncpus()` uses platform-specific code to determine the number of available CPUs for some specific systems. This functionality is now available in the standard `os` module as `cpu_count()`.

Component: **misc**

Author: **Lorenz Panny**

Branch/Commit: **[public/change_ncpus_to_os_module](https://github.com/sagemath/sagetrac-mirror/tree/public/change_ncpus_to_os_module) @ [`a509210`](https://github.com/sagemath/sagetrac-mirror/commit/a509210125fc50baf72dcb7f2248e96cddf61c8f)**

_Issue created by migration from https://trac.sagemath.org/ticket/34328_



--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `src/sage/parallel/ncpus.py`
Content:
```
1 """
2 CPU Detection
3 """
4 # Parallel Python Software: http://www.parallelpython.com
5 # Copyright (c) 2005-2008, Vitalii Vanovschi
6 # All rights reserved.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are met:
9 #    * Redistributions of source code must retain the above copyright notice,
10 #      this list of conditions and the following disclaimer.
11 #    * Redistributions in binary form must reproduce the above copyright
12 #      notice, this list of conditions and the following disclaimer in the
13 #      documentation and/or other materials provided with the distribution.
14 #    * Neither the name of the author nor the names of its contributors
15 #      may be used to endorse or promote products derived from this software
16 #      without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 # THE POSSIBILITY OF SUCH DAMAGE.
29 
30 ######
31 # This is from ParallelPython (the pp.py file).
32 
33 import os
34 import subprocess
35 
36 
37 def ncpus():
38     """
39     Detects the number of effective CPUs in the system.
40 
41     EXAMPLES::
42 
43         sage: sage.parallel.ncpus.ncpus()  # random output -- depends on machine.
44         2
45     """
46     # Support Sage environment variable SAGE_NUM_THREADS
47     # NOTE: while doctesting, this is forced to be 2 by the
48     # sage-runtests script
49     try:
50         n = os.environ["SAGE_NUM_THREADS"]
51     except KeyError:
52         pass
53     else:
54         return int(n)
55 
56     # for Linux, Unix and MacOS
57     if hasattr(os, "sysconf"):
58         if "SC_NPROCESSORS_ONLN" in os.sysconf_names:
59             # Linux and Unix
60             ncpus = os.sysconf("SC_NPROCESSORS_ONLN")
61             if isinstance(ncpus, int) and ncpus > 0:
62                 return ncpus
63         else:
64             # MacOS X
65             # deprecated: return int(os.popen2("sysctl -n hw.ncpu")[1].read())
66             process = subprocess.Popen("sysctl -n hw.ncpu", shell=True,
67                                        stdin=subprocess.PIPE,
68                                        stdout=subprocess.PIPE,
69                                        stderr=subprocess.PIPE, close_fds=True)
70             return int(process.stdout.read())
71     # for Windows
72     if "NUMBER_OF_PROCESSORS" in os.environ:
73         ncpus = int(os.environ["NUMBER_OF_PROCESSORS"])
74         if ncpus > 0:
75             return ncpus
76     # return the default value
77     return 1
78
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/src/sage/parallel/ncpus.py b/src/sage/parallel/ncpus.py
--- a/src/sage/parallel/ncpus.py
+++ b/src/sage/parallel/ncpus.py
@@ -1,46 +1,19 @@
 """
 CPU Detection
 """
-# Parallel Python Software: http://www.parallelpython.com
-# Copyright (c) 2005-2008, Vitalii Vanovschi
-# All rights reserved.
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions are met:
-#    * Redistributions of source code must retain the above copyright notice,
-#      this list of conditions and the following disclaimer.
-#    * Redistributions in binary form must reproduce the above copyright
-#      notice, this list of conditions and the following disclaimer in the
-#      documentation and/or other materials provided with the distribution.
-#    * Neither the name of the author nor the names of its contributors
-#      may be used to endorse or promote products derived from this software
-#      without specific prior written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
-# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
-# THE POSSIBILITY OF SUCH DAMAGE.
-
-######
-# This is from ParallelPython (the pp.py file).
 
 import os
-import subprocess
 
 
 def ncpus():
     """
-    Detects the number of effective CPUs in the system.
+    Return the number of available CPUs in the system.
+
+    ALGORITHM: :func:`os.sched_getaffinity` or :func:`os.cpu_count`
 
     EXAMPLES::
 
-        sage: sage.parallel.ncpus.ncpus()  # random output -- depends on machine.
+        sage: sage.parallel.ncpus.ncpus()  # random output -- depends on machine
         2
     """
     # Support Sage environment variable SAGE_NUM_THREADS
@@ -53,25 +26,9 @@
     else:
         return int(n)
 
-    # for Linux, Unix and MacOS
-    if hasattr(os, "sysconf"):
-        if "SC_NPROCESSORS_ONLN" in os.sysconf_names:
-            # Linux and Unix
-            ncpus = os.sysconf("SC_NPROCESSORS_ONLN")
-            if isinstance(ncpus, int) and ncpus > 0:
-                return ncpus
-        else:
-            # MacOS X
-            # deprecated: return int(os.popen2("sysctl -n hw.ncpu")[1].read())
-            process = subprocess.Popen("sysctl -n hw.ncpu", shell=True,
-                                       stdin=subprocess.PIPE,
-                                       stdout=subprocess.PIPE,
-                                       stderr=subprocess.PIPE, close_fds=True)
-            return int(process.stdout.read())
-    # for Windows
-    if "NUMBER_OF_PROCESSORS" in os.environ:
-        ncpus = int(os.environ["NUMBER_OF_PROCESSORS"])
-        if ncpus > 0:
-            return ncpus
-    # return the default value
-    return 1
+    n = None
+
+    if hasattr(os, 'sched_getaffinity'):
+        n = len(os.sched_getaffinity(0))
+
+    return n or os.cpu_count() or 1

{"golden_diff": "diff --git a/src/sage/parallel/ncpus.py b/src/sage/parallel/ncpus.py\n--- a/src/sage/parallel/ncpus.py\n+++ b/src/sage/parallel/ncpus.py\n@@ -1,46 +1,19 @@\n \"\"\"\n CPU Detection\n \"\"\"\n-# Parallel Python Software: http://www.parallelpython.com\n-# Copyright (c) 2005-2008, Vitalii Vanovschi\n-# All rights reserved.\n-# Redistribution and use in source and binary forms, with or without\n-# modification, are permitted provided that the following conditions are met:\n-#    * Redistributions of source code must retain the above copyright notice,\n-#      this list of conditions and the following disclaimer.\n-#    * Redistributions in binary form must reproduce the above copyright\n-#      notice, this list of conditions and the following disclaimer in the\n-#      documentation and/or other materials provided with the distribution.\n-#    * Neither the name of the author nor the names of its contributors\n-#      may be used to endorse or promote products derived from this software\n-#      without specific prior written permission.\n-#\n-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n-# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n-# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n-# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n-# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n-# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n-# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n-# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n-# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n-# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF\n-# THE POSSIBILITY OF SUCH DAMAGE.\n-\n-######\n-# This is from ParallelPython (the pp.py file).\n \n import os\n-import subprocess\n \n \n def ncpus():\n     \"\"\"\n-    Detects the number of effective CPUs in the system.\n+    Return the number of available CPUs in the system.\n+\n+    ALGORITHM: :func:`os.sched_getaffinity` or :func:`os.cpu_count`\n \n     EXAMPLES::\n \n-        sage: sage.parallel.ncpus.ncpus()  # random output -- depends on machine.\n+        sage: sage.parallel.ncpus.ncpus()  # random output -- depends on machine\n         2\n     \"\"\"\n     # Support Sage environment variable SAGE_NUM_THREADS\n@@ -53,25 +26,9 @@\n     else:\n         return int(n)\n \n-    # for Linux, Unix and MacOS\n-    if hasattr(os, \"sysconf\"):\n-        if \"SC_NPROCESSORS_ONLN\" in os.sysconf_names:\n-            # Linux and Unix\n-            ncpus = os.sysconf(\"SC_NPROCESSORS_ONLN\")\n-            if isinstance(ncpus, int) and ncpus > 0:\n-                return ncpus\n-        else:\n-            # MacOS X\n-            # deprecated: return int(os.popen2(\"sysctl -n hw.ncpu\")[1].read())\n-            process = subprocess.Popen(\"sysctl -n hw.ncpu\", shell=True,\n-                                       stdin=subprocess.PIPE,\n-                                       stdout=subprocess.PIPE,\n-                                       stderr=subprocess.PIPE, close_fds=True)\n-            return int(process.stdout.read())\n-    # for Windows\n-    if \"NUMBER_OF_PROCESSORS\" in os.environ:\n-        ncpus = int(os.environ[\"NUMBER_OF_PROCESSORS\"])\n-        if ncpus > 0:\n-            return ncpus\n-    # return the default value\n-    return 1\n+    n = None\n+\n+    if hasattr(os, 'sched_getaffinity'):\n+        n = len(os.sched_getaffinity(0))\n+\n+    return n or os.cpu_count() or 1\n", "issue": "make sage.parallel.ncpus.ncpus() use os.cpu_count()\n<div id=\"comment:0\"></div>\n\nCurrently, `sage.parallel.ncpus.ncpus()` uses platform-specific code to determine the number of available CPUs for some specific systems. This functionality is now available in the standard `os` module as `cpu_count()`.\n\nComponent: **misc**\n\nAuthor: **Lorenz Panny**\n\nBranch/Commit: **[public/change_ncpus_to_os_module](https://github.com/sagemath/sagetrac-mirror/tree/public/change_ncpus_to_os_module) @ [`a509210`](https://github.com/sagemath/sagetrac-mirror/commit/a509210125fc50baf72dcb7f2248e96cddf61c8f)**\n\n_Issue created by migration from https://trac.sagemath.org/ticket/34328_\n\n\n", "before_files": [{"content": "\"\"\"\nCPU Detection\n\"\"\"\n# Parallel Python Software: http://www.parallelpython.com\n# Copyright (c) 2005-2008, Vitalii Vanovschi\n# All rights reserved.\n# Redistribution and use in source and binary forms, with or without\n# modification, are permitted provided that the following conditions are met:\n#    * Redistributions of source code must retain the above copyright notice,\n#      this list of conditions and the following disclaimer.\n#    * Redistributions in binary form must reproduce the above copyright\n#      notice, this list of conditions and the following disclaimer in the\n#      documentation and/or other materials provided with the distribution.\n#    * Neither the name of the author nor the names of its contributors\n#      may be used to endorse or promote products derived from this software\n#      without specific prior written permission.\n#\n# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF\n# THE POSSIBILITY OF SUCH DAMAGE.\n\n######\n# This is from ParallelPython (the pp.py file).\n\nimport os\nimport subprocess\n\n\ndef ncpus():\n    \"\"\"\n    Detects the number of effective CPUs in the system.\n\n    EXAMPLES::\n\n        sage: sage.parallel.ncpus.ncpus()  # random output -- depends on machine.\n        2\n    \"\"\"\n    # Support Sage environment variable SAGE_NUM_THREADS\n    # NOTE: while doctesting, this is forced to be 2 by the\n    # sage-runtests script\n    try:\n        n = os.environ[\"SAGE_NUM_THREADS\"]\n    except KeyError:\n        pass\n    else:\n        return int(n)\n\n    # for Linux, Unix and MacOS\n    if hasattr(os, \"sysconf\"):\n        if \"SC_NPROCESSORS_ONLN\" in os.sysconf_names:\n            # Linux and Unix\n            ncpus = os.sysconf(\"SC_NPROCESSORS_ONLN\")\n            if isinstance(ncpus, int) and ncpus > 0:\n                return ncpus\n        else:\n            # MacOS X\n            # deprecated: return int(os.popen2(\"sysctl -n hw.ncpu\")[1].read())\n            process = subprocess.Popen(\"sysctl -n hw.ncpu\", shell=True,\n                                       stdin=subprocess.PIPE,\n                                       stdout=subprocess.PIPE,\n                                       stderr=subprocess.PIPE, close_fds=True)\n            return int(process.stdout.read())\n    # for Windows\n    if \"NUMBER_OF_PROCESSORS\" in os.environ:\n        ncpus = int(os.environ[\"NUMBER_OF_PROCESSORS\"])\n        if ncpus > 0:\n            return ncpus\n    # return the default value\n    return 1\n", "path": "src/sage/parallel/ncpus.py"}], "after_files": [{"content": "\"\"\"\nCPU Detection\n\"\"\"\n\nimport os\n\n\ndef ncpus():\n    \"\"\"\n    Return the number of available CPUs in the system.\n\n    ALGORITHM: :func:`os.sched_getaffinity` or :func:`os.cpu_count`\n\n    EXAMPLES::\n\n        sage: sage.parallel.ncpus.ncpus()  # random output -- depends on machine\n        2\n    \"\"\"\n    # Support Sage environment variable SAGE_NUM_THREADS\n    # NOTE: while doctesting, this is forced to be 2 by the\n    # sage-runtests script\n    try:\n        n = os.environ[\"SAGE_NUM_THREADS\"]\n    except KeyError:\n        pass\n    else:\n        return int(n)\n\n    n = None\n\n    if hasattr(os, 'sched_getaffinity'):\n        n = len(os.sched_getaffinity(0))\n\n    return n or os.cpu_count() or 1\n", "path": "src/sage/parallel/ncpus.py"}]}