From 2847d7431d9334220445e93dc7af707821e80d5e Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 4 Feb 2017 23:33:20 +1100 Subject: [PATCH] tests/thread: Replace busy waiting loops with a loop that sleeps. Depending on the thread scheduler, a busy-wait loop can hog the CPU and make the tests very slow. So convert such loops to loops that have an explicit sleep, allowing the worker threads to do their job. --- tests/thread/stress_heap.py | 8 ++++++-- tests/thread/thread_lock4.py | 5 +++++ tests/thread/thread_qstr1.py | 8 ++++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/tests/thread/stress_heap.py b/tests/thread/stress_heap.py index ac3ebe0491..5482a9ac6f 100644 --- a/tests/thread/stress_heap.py +++ b/tests/thread/stress_heap.py @@ -3,6 +3,10 @@ # # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd +try: + import utime as time +except ImportError: + import time import _thread def last(l): @@ -37,6 +41,6 @@ n_finished = 0 for i in range(n_thread): _thread.start_new_thread(thread_entry, (10000,)) -# busy wait for threads to finish +# wait for threads to finish while n_finished < n_thread: - pass + time.sleep(1) diff --git a/tests/thread/thread_lock4.py b/tests/thread/thread_lock4.py index d77aa24ee8..2f9d42d6b5 100644 --- a/tests/thread/thread_lock4.py +++ b/tests/thread/thread_lock4.py @@ -2,6 +2,10 @@ # # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd +try: + import utime as time +except ImportError: + import time import _thread def fac(n): @@ -39,6 +43,7 @@ while True: with jobs_lock: if len(output) == n_jobs: break + time.sleep(1) # sort and print the results output.sort(key=lambda x: x[0]) diff --git a/tests/thread/thread_qstr1.py b/tests/thread/thread_qstr1.py index c0256316e5..f4136d9646 100644 --- a/tests/thread/thread_qstr1.py +++ b/tests/thread/thread_qstr1.py @@ -2,6 +2,10 @@ # # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd +try: + import utime as time +except ImportError: + import time import _thread # function to check the interned string @@ -28,8 +32,8 @@ n_qstr_per_thread = 100 # make 1000 for a more stressful test (uses more heap) for i in range(n_thread): _thread.start_new_thread(th, (i * n_qstr_per_thread, n_qstr_per_thread)) -# busy wait for threads to finish +# wait for threads to finish while n_finished < n_thread: - pass + time.sleep(1) print('pass')