8000 sleep in main thread to yield · daq-tools/pycom-micropython@237a8fe · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 237a8fe

Browse files
committed
sleep in main thread to yield
otherwise other threads starve
1 parent 77d25aa commit 237a8fe

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')

tests/thread/thread_lock3.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
lock = _thread.allocate_lock()
89
n_thread = 10
@@ -24,4 +25,4 @@ def thread_entry(idx):
2425

2526
# busy wait for threads to finish
2627
while n_finished < n_thread:
27-
pass
28+
time.sleep(0.01)

tests/thread/thread_shared1.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(i):
89
pass
@@ -27,5 +28,5 @@ def thread_entry(n, tup):
2728

2829
# busy wait for threads to finish
2930
while n_finished < n_thread:
30-
pass
31+
time.sleep(0.01)
3132
print(tup)

tests/thread/thread_shared2.py

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

66
import _thread
7+
import time
78

89
def foo(lst, i):
910
lst[i] += 1
@@ -28,5 +29,5 @@ def thread_entry(n, lst, idx):
2829

2930
# busy wait for threads to finish
3031
while n_finished < n_thread:
31-
pass
32+
time.sleep(0.01)
3233
print(lst)

tests/thread/thread_stacksize1.py

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

55
import sys
66
import _thread
7+
import time
78

89
# different implementations have different minimum sizes
910
if sys.implementation.name == 'micropython':
@@ -43,5 +44,5 @@ def thread_entry():
4344

4445
# busy wait for threads to finish
4546
while n_finished < n_thread:
46-
pass
47+
time.sleep(0.01)
4748
print('done')

0 commit comments

Comments
 (0)
0