user+sys time bigger than real time (with threads)
Marco Buttu
marco.buttu at gmail.com
Thu Feb 2 11:31:18 EST 2017
Let's have the following example:
$ cat foo.py
from threading import Thread, Lock
result = 0
lock = Lock()
def task():
global result
for i in range(10**6):
lock.acquire()
result += 1
lock.release()
if __name__ == '__main__':
t1, t2 = Thread(target=task), Thread(target=task)
t1.start()
t2.start()
t1.join()
t2.join()
print('result:', result)
When I execute it (Python 3.6), I get a sys+user time bigger than the
real time:
$ time python foo.py
result: 2000000
real 0m7.088s
user 0m6.597s
sys 0m5.043s
I can expect this result when I run some processes in parallel on
different CPUs, but this code uses threads, so the GIL prevents the two
task() functions to be executed in parallel. What am I missing?
--
Marco Buttu
INAF-Osservatorio Astronomico di Cagliari
Via della Scienza n. 5, 09047 Selargius (CA)
Phone: 070 711 80 217
Email: mbuttu at oa-cagliari.inaf.it
More information about the Python-list
mailing list