2003-07-04 14:24:44 +00:00
|
|
|
# -*- coding: iso-8859-1 -*-
|
2009-01-08 14:18:03 +00:00
|
|
|
# Copyright (C) 2000-2009 Bastian Kleineidam
|
2001-03-15 01:19:35 +00:00
|
|
|
#
|
2001-05-23 21:20:44 +00:00
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
2001-03-15 01:19:35 +00:00
|
|
|
#
|
2001-05-23 21:20:44 +00:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
2001-03-15 01:19:35 +00:00
|
|
|
#
|
2001-05-23 21:20:44 +00:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
|
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2005-01-19 15:08:02 +00:00
|
|
|
"""
|
2005-03-04 23:18:10 +00:00
|
|
|
Support for managing threads.
|
2005-01-19 15:08:02 +00:00
|
|
|
"""
|
|
|
|
|
|
2005-03-04 23:18:10 +00:00
|
|
|
import os
|
2006-05-24 22:29:22 +00:00
|
|
|
import threading
|
2005-03-04 23:18:10 +00:00
|
|
|
try:
|
|
|
|
|
import win32process
|
2008-06-05 07:14:04 +00:00
|
|
|
has_win32process = True
|
2005-03-04 23:18:10 +00:00
|
|
|
except ImportError:
|
2008-06-05 07:14:04 +00:00
|
|
|
has_win32process = False
|
|
|
|
|
from .containers import enum
|
2005-03-04 23:18:10 +00:00
|
|
|
|
|
|
|
|
# generic thread priorities with mappings to Windows and Unix
|
|
|
|
|
# priority values
|
2008-06-05 07:14:04 +00:00
|
|
|
Prio = enum("high", "normal", "low")
|
2005-03-04 23:18:10 +00:00
|
|
|
|
|
|
|
|
_posix_nice_val = {
|
2008-06-05 07:14:04 +00:00
|
|
|
Prio.high: -5,
|
|
|
|
|
Prio.normal: +0,
|
|
|
|
|
Prio.low: +10,
|
2005-03-04 23:18:10 +00:00
|
|
|
}
|
2008-06-05 07:14:04 +00:00
|
|
|
if has_win32process:
|
2005-03-04 23:18:10 +00:00
|
|
|
if hasattr(win32process, "BELOW_NORMAL_PRIORITY_CLASS"):
|
2008-06-05 07:14:04 +00:00
|
|
|
low = win32process.BELOW_NORMAL_PRIORITY_CLASS
|
2005-03-04 23:18:10 +00:00
|
|
|
else:
|
2008-06-05 07:14:04 +00:00
|
|
|
low = win32process.IDLE_PRIORITY_CLASS
|
2005-03-04 23:18:10 +00:00
|
|
|
if hasattr(win32process, "ABOVE_NORMAL_PRIORITY_CLASS"):
|
2008-06-05 07:14:04 +00:00
|
|
|
high = win32process.ABOVE_NORMAL_PRIORITY_CLASS
|
2005-03-04 23:18:10 +00:00
|
|
|
else:
|
2008-06-05 07:14:04 +00:00
|
|
|
high = win32process.HIGH_PRIORITY_CLASS
|
2005-03-04 23:18:10 +00:00
|
|
|
_nt_prio_val = {
|
2008-06-05 07:14:04 +00:00
|
|
|
Prio.high: high,
|
|
|
|
|
Prio.normal: win32process.NORMAL_PRIORITY_CLASS,
|
|
|
|
|
Prio.low: low,
|
2005-03-04 23:18:10 +00:00
|
|
|
}
|
2008-06-05 07:14:04 +00:00
|
|
|
del low, high
|
2005-03-04 23:18:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def set_thread_priority (prio):
|
2008-04-27 11:39:21 +00:00
|
|
|
"""Set priority of this thread (and thus also for all spawned threads)."""
|
2008-06-05 07:14:04 +00:00
|
|
|
if os.name == 'nt' and has_win32process:
|
2005-03-04 23:18:10 +00:00
|
|
|
res = win32process.SetPriorityClass(
|
|
|
|
|
win32process.GetCurrentProcess(), _nt_prio_val[prio])
|
|
|
|
|
elif os.name == 'posix':
|
|
|
|
|
res = os.nice(_posix_nice_val[prio])
|
|
|
|
|
else:
|
|
|
|
|
res = None
|
|
|
|
|
return res
|
|
|
|
|
|
2006-05-24 22:29:22 +00:00
|
|
|
|
|
|
|
|
class StoppableThread (threading.Thread):
|
2008-04-27 11:39:21 +00:00
|
|
|
"""Thread class with a stop() method. The thread itself has to check
|
|
|
|
|
regularly for the stopped() condition."""
|
2006-05-24 22:29:22 +00:00
|
|
|
|
|
|
|
|
def __init__ (self):
|
|
|
|
|
super(StoppableThread, self).__init__()
|
|
|
|
|
self._stop = threading.Event()
|
|
|
|
|
|
|
|
|
|
def stop (self):
|
|
|
|
|
self._stop.set()
|
|
|
|
|
|
|
|
|
|
def stopped (self):
|
|
|
|
|
return self._stop.isSet()
|