grab-cores.py: Use threading.Thread to call spam.busy
authorW. Trevor King <wking@tremily.us>
Wed, 17 Apr 2013 16:15:49 +0000 (12:15 -0400)
committerW. Trevor King <wking@tremily.us>
Wed, 17 Apr 2013 16:17:47 +0000 (12:17 -0400)
Usually Python threads can only use one core at a time due to the
Python global interpreter lock.  This script allows us to see if
dropping the GIL with Py_BEGIN_ALLOW_THREADS allows us to use multiple
cores simultaneously.

grab-cores.py [new file with mode: 0755]

diff --git a/grab-cores.py b/grab-cores.py
new file mode 100755 (executable)
index 0000000..0815443
--- /dev/null
@@ -0,0 +1,21 @@
+#!/usr/bin/env python
+
+import threading
+
+import spam
+
+
+def grab_cores(threads=1, count=int(1e9)):
+    _threads = []
+    for i in range(threads):
+        thread = threading.Thread(target=spam.busy, args=(count,))
+        _threads.append(thread)
+        thread.start()
+    for thread in _threads:
+        thread.join()
+
+
+if __name__ == '__main__':
+    import sys
+
+    grab_cores(threads=int(sys.argv[1]))