Skip to content

Commit 968e19a

Browse files
committed
libasynctasks: better handling of exceptions for all threads...
1 parent da3d811 commit 968e19a

2 files changed

Lines changed: 21 additions & 15 deletions

File tree

src/_threading.pyx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import time
2+
3+
from cpython.exc cimport PyErr_CheckSignals
4+
5+
6+
def thread_wait():
7+
"""
8+
Wait for a short amount of time before executing anymore
9+
code on the thread, this reduces overall idle CPU usage...
10+
"""
11+
12+
# check for Python error signals so we can except
13+
# errors like KeyboardInterrupt, SystemExit etc...
14+
PyErr_CheckSignals()
15+
16+
# hault the current thread for a short amount of time,
17+
# so that we use less CPU usage when idling to free up the
18+
# CPU for other running processes...
19+
time.sleep(0.0001)

src/libasynctasks.pyx

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ A framework built to make concurrency fast, simple and dynamic;
33
using the Task concept (and soon to support coroutines async/await...).
44
"""
55

6-
from cpython.exc cimport PyErr_CheckSignals
7-
86
import threading
97
import time
108
import collections
119

10+
include "_threading.pyx"
11+
1212

1313
ctypedef enum TaskResult:
1414
TASK_DONE,
@@ -503,10 +503,6 @@ cdef class TaskManager(object):
503503
except (KeyboardInterrupt, SystemExit):
504504
break
505505

506-
# check for Python error signals so we can except
507-
# errors like KeyboardInterrupt, SystemExit etc...
508-
PyErr_CheckSignals()
509-
510506
thread_wait()
511507

512508
self.destroy()
@@ -516,12 +512,3 @@ cdef class TaskManager(object):
516512
self._task_queue = None
517513

518514
super().__del__()
519-
520-
521-
cdef void thread_wait():
522-
"""
523-
Wait for a short amount of time before executing anymore
524-
code on the thread, this reduces overall idle CPU usage...
525-
"""
526-
527-
time.sleep(0.0001)

0 commit comments

Comments
 (0)