Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit bbf355d

Browse files
authored
Merge pull request #105 from pycom/fix_thread_tests
sleep in main thread to yield
2 parents 77d25aa + 237a8fe commit bbf355d

14 files changed

+27
-15
lines changed

tests/thread/mutate_bytearray.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
44

55
import _thread
6+
import time
67

78
# the shared bytearray
89
ba = bytearray()
@@ -34,12 +35,11 @@ def th(n, lo, hi):
3435

3536
# busy wait for threads to finish
3637
while n_finished < n_thread:
37-
pass
38+
time.sleep(0.01)
3839

3940
# check bytearray has correct contents
4041
print(len(ba))
4142
count = [0 for _ in range(256)]
4243
for b in ba:
4344
count[b] += 1
4445
print(count)
45-

tests/thread/mutate_dict.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
44

55
import _thread
6+
import time
67

78
# the shared dict
89
di = {'a':'A', 'b':'B', 'c':'C', 'd':'D'}
@@ -36,7 +37,7 @@ def th(n, lo, hi):
3637

3738
# busy wait for threads to finish
3839
while n_finished < n_thread:
39-
pass
40+
time.sleep(0.01)
4041

4142
# check dict has correct contents
4243
print(sorted(di.items()))

tests/thread/mutate_instance.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
44

55
import _thread
6+
import time
67

78
# the shared user class and instance
89
class User:
@@ -35,7 +36,7 @@ def th(n, lo, hi):
3536

3637
# busy wait for threads to finish
3738
while n_finished < n_thread:
38-
pass
39+
time.sleep(0.01)
3940

4041
# check user instance has correct contents
4142
print(user.a, user.b, user.c)

tests/thread/mutate_list.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
44

55
import _thread
6+
import time
67

78
# the shared list
89
li = list()
@@ -37,7 +38,7 @@ def th(n, lo, hi):
3738

3839
# busy wait for threads to finish
3940
while n_finished < n_thread:
40-
pass
41+
time.sleep(0.01)
4142

4243
# check list has correct contents
4344
li.sort()

tests/thread/mutate_set.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
44

55
import _thread
6+
import time
67

78
# the shared set
89
se = set([-1, -2, -3, -4])
@@ -31,7 +32,7 @@ def th(n, lo, hi):
3132

3233
# busy wait for threads to finish
3334
while n_finished < n_thread:
34-
pass
35+
time.sleep(0.01)
3536

3637
# check set has correct contents
3738
print(sorted(se))

tests/thread/stress_create.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def thread_entry(n):
1515
_thread.start_new_thread(thread_entry, (thread_num,))
1616
thread_num += 1
1717
except MemoryError:
18-
pass
18+
time.sleep(0.01)
1919

2020
# wait for the last threads to terminate
2121
time.sleep(1)

tests/thread/stress_recurse.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
44

55
import _thread
6+
import time
67

78
def foo():
89
foo()
@@ -21,5 +22,5 @@ def thread_entry():
2122

2223
# busy wait for thread to finish
2324
while not finished:
24-
pass
25+
time.sleep(0.01)
2526
print('done')

tests/thread/thread_exc1.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
44

55
import _thread
6+
import time
67

78
def foo():
89
raise ValueError
@@ -26,5 +27,5 @@ def thread_entry():
2627

2728
# busy wait for threads to finish
2829
while n_finished < n_thread:
29-
pass
30+
time.sleep(0.01)
3031
print('done')

tests/thread/thread_gc1.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import gc
66
import _thread
7+
import time
78

89
def thread_entry(n):
910
# allocate a bytearray and fill it
@@ -31,4 +32,4 @@ def thread_entry(n):
3132

3233
# busy wait for threads to finish
3334
while n_finished < n_thread:
34-
pass
35+
time.sleep(0.01)

tests/thread/thread_ident1.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd
44

55
import _thread
6+
import time
67

78
def thread_entry():
89
tid = _thread.get_ident()
@@ -17,5 +18,5 @@ def thread_entry():
1718
_thread.start_new_thread(thread_entry, ())
1819

1920
while not finished:
20-
pass
21+
time.sleep(0.01)
2122
print('done')

0 commit comments

Comments
 (0)