def __call__(self, *args, **kwargs):
if constant.MAIN_UI is None:
raise RuntimeError('Main UI not initialized')
# Check if this was called in the main thread (the one running Qt EventLoop)
# noinspection PyProtectedMember
if isinstance(threading.current_thread(), threading._MainThread):
# If we are in the EventLoop, there's no need to dispatch the call to the a signal
# noinspection PyProtectedMember
constant.MAIN_UI._do('main_ui', self.func.__name__, args, kwargs)
else:
# Otherwise, queue the call
constant.MAIN_UI.do('main_ui', self.func.__name__, *args, **kwargs)
# noinspection PyAbstractClass
python类current_thread()的实例源码
def close(self):
"""
Closes our socket connection. This is a pass-through for our socket's
:func:`~stem.socket.ControlSocket.close` method.
"""
self._socket.close()
# Join on any outstanding state change listeners. Closing is a state change
# of its own, so if we have any listeners it's quite likely there's some
# work in progress.
#
# It's important that we do this outside of our locks so those daemons have
# access to us. This is why we're doing this here rather than _close().
for t in self._state_change_threads:
if t.is_alive() and threading.current_thread() != t:
t.join()
def _close(self):
# Our is_alive() state is now false. Our reader thread should already be
# awake from recv() raising a closure exception. Wake up the event thread
# too so it can end.
self._event_notice.set()
self._is_authenticated = False
# joins on our threads if it's safe to do so
for t in (self._reader_thread, self._event_thread):
if t and t.is_alive() and threading.current_thread() != t:
t.join()
self._notify_status_listeners(State.CLOSED)
self._socket_close()
def startup(self):
"Notify and wait frontend to set initial params and breakpoints"
# send some useful info to identify session
thread = threading.current_thread()
# get the caller module filename
frame = sys._getframe()
fn = self.canonic(frame.f_code.co_filename)
while frame.f_back and self.canonic(frame.f_code.co_filename) == fn:
frame = frame.f_back
args = [__version__, os.getpid(), thread.name, " ".join(sys.argv),
frame.f_code.co_filename]
self.pipe.send({'method': 'startup', 'args': args})
while self.pull_actions() is not None:
pass
# General interaction function
def fetch_current_thread_functor():
"""Get the current thread.
If eventlet is used to monkey-patch the threading module, return the
current eventlet greenthread. Otherwise, return the current Python thread.
.. versionadded:: 1.5
"""
# Until https://github.com/eventlet/eventlet/issues/172 is resolved
# or addressed we have to use complicated workaround to get a object
# that will not be recycled; the usage of threading.current_thread()
# doesn't appear to currently be monkey patched and therefore isn't
# reliable to use (and breaks badly when used as all threads share
# the same current_thread() object)...
if not EVENTLET_AVAILABLE:
return threading.current_thread
else:
green_threaded = _patcher.is_monkey_patched('thread')
if green_threaded:
return _eventlet.getcurrent
else:
return threading.current_thread
def schedule_thread_worker(schedule, logger):
''' schedule thread, takes care of running processes in the future '''
global CTRL_C
logLine = 'starting thread_worker'
logger.debug(logLine)
while not CTRL_C['STOP']:
#print('looping', CTRL_C)
sys.stdout.flush()
schedule.run_pending()
logLine = 'scheduler woke {0}'.format(
threading.current_thread().getName())
time.sleep(1)
logger.debug(logLine)
logger.debug('Threading stop:{0}'.format(
threading.current_thread().getName()))
sys.exit()
def _multi_thread_download(url, file_name, file_size, thread_count):
import threading
fp = open(file_name, "wb")
fp.truncate(file_size)
fp.close()
part = file_size // thread_count
for i in range(thread_count):
start = part * i
if i == thread_count - 1:
end = file_size
else:
end = start + part
t = threading.Thread(target=_downloader, kwargs={'start': start, 'end': end, 'url': url, 'filename': file_name})
t.setDaemon(True)
t.start()
main_thread = threading.current_thread()
for t in threading.enumerate():
if t is main_thread:
continue
t.join()
return file_name
def __init__(self, path, threaded=True):
"""
>>> lock = LockBase("somefile")
>>> lock = LockBase("somefile", threaded=False)
"""
self.path = path
self.lock_file = os.path.abspath(path) + ".lock"
self.hostname = socket.gethostname()
self.pid = os.getpid()
app_name = "-test"
if threaded:
name = threading.current_thread().get_name()
tname = "%s-" % quote(name, safe="")
else:
tname = ""
dirname = os.path.dirname(self.lock_file)
self.unique_name = os.path.join(dirname,
"%s%s.%s%s" % (self.hostname,
app_name,
tname,
self.pid))
msg = " ".join(["lock_file:", self.lock_file])
logger.debug(msg)
msg = " ".join(["unique_name:", self.unique_name])
logger.debug(msg)
def _thread_wrapper(self, *args):
''' Wrapper for the worker method defined in the module. Handles calling the actual worker, cleanly exiting upon
interrupt, and passing exceptions back to the main process.'''
thread_name = threading.current_thread().name
self.debug('THREAD => %s started.' % thread_name)
while not self.stopped.is_set():
try:
# use the get_nowait() method for retrieving a queued item to
# prevent the thread from blocking when the queue is empty
obj = self.q.get_nowait()
except Empty:
continue
try:
# launch the public module_thread method
self.module_thread(obj, *args)
except:
# handle exceptions local to the thread
self.print_exception('(thread=%s, object=%s)' % (thread_name, repr(obj)))
finally:
self.q.task_done()
self.debug('THREAD => %s exited.' % thread_name)
# sometimes a keyboardinterrupt causes a race condition between when the self.q.task_done() call above and the
# self.q.empty() call below, causing all the threads to hang. introducing the time.sleep(.7) call below reduces
# the likelihood of encountering the race condition.
def scheduler():
return Pycos._schedulers.get(id(threading.current_thread()), None)
def cur_task(scheduler=None):
"""Must be called from a task only.
"""
if not scheduler:
scheduler = Pycos._schedulers.get(id(threading.current_thread()), None)
if not scheduler:
return None
return scheduler.__cur_task
def scheduler():
return Pycos._schedulers.get(id(threading.current_thread()), None)
def cur_task(scheduler=None):
"""Must be called from a task only.
"""
if not scheduler:
scheduler = Pycos._schedulers.get(id(threading.current_thread()), None)
if not scheduler:
return None
return scheduler.__cur_task
def exit(code=0):
preloader.stop()
if threading.current_thread().__class__.__name__ == '_MainThread':
sys.exit(code)
else:
thread.exit()
def __init__(self, path, threaded=True, timeout=None):
"""
>>> lock = LockBase('somefile')
>>> lock = LockBase('somefile', threaded=False)
"""
super(LockBase, self).__init__(path)
self.lock_file = os.path.abspath(path) + ".lock"
self.hostname = socket.gethostname()
self.pid = os.getpid()
if threaded:
t = threading.current_thread()
# Thread objects in Python 2.4 and earlier do not have ident
# attrs. Worm around that.
ident = getattr(t, "ident", hash(t))
self.tname = "-%x" % (ident & 0xffffffff)
else:
self.tname = ""
dirname = os.path.dirname(self.lock_file)
# unique name is mostly about the current process, but must
# also contain the path -- otherwise, two adjacent locked
# files conflict (one file gets locked, creating lock-file and
# unique file, the other one gets locked, creating lock-file
# and overwriting the already existing lock-file, then one
# gets unlocked, deleting both lock-file and unique file,
# finally the last lock errors out upon releasing.
self.unique_name = os.path.join(dirname,
"%s%s.%s%s" % (self.hostname,
self.tname,
self.pid,
hash(self.path)))
self.timeout = timeout
def __init__(self, path, threaded=True, timeout=None):
"""
>>> lock = LockBase('somefile')
>>> lock = LockBase('somefile', threaded=False)
"""
super(LockBase, self).__init__(path)
self.lock_file = os.path.abspath(path) + ".lock"
self.hostname = socket.gethostname()
self.pid = os.getpid()
if threaded:
t = threading.current_thread()
# Thread objects in Python 2.4 and earlier do not have ident
# attrs. Worm around that.
ident = getattr(t, "ident", hash(t))
self.tname = "-%x" % (ident & 0xffffffff)
else:
self.tname = ""
dirname = os.path.dirname(self.lock_file)
# unique name is mostly about the current process, but must
# also contain the path -- otherwise, two adjacent locked
# files conflict (one file gets locked, creating lock-file and
# unique file, the other one gets locked, creating lock-file
# and overwriting the already existing lock-file, then one
# gets unlocked, deleting both lock-file and unique file,
# finally the last lock errors out upon releasing.
self.unique_name = os.path.join(dirname,
"%s%s.%s%s" % (self.hostname,
self.tname,
self.pid,
hash(self.path)))
self.timeout = timeout
def execute(self):
# Parse arguments.
self.arguments = self.parser.parse_args()
# Initiate the logger.
threading.current_thread().setName('Main')
initiate_logger()
self.logger = logging.getLogger(__name__)
# Initiate the settings by accessing one.
self.logger.debug('Initiated configuration and environment... (debug on, means no error reporting and verbose output')
if not settings.DEBUG:
self.logger.info('Initiated configuration and environment...')
self.logger.info('-------------------------------[ PyPlanet v{} ]-------------------------------'.format(version))
# Start god process (the current started process).
pool = EnvironmentPool(settings.POOLS, max_restarts=self.arguments.max_restarts)
pool.populate()
# Starting all processes.
pool.start()
# Start the watchdog.
try:
pool.watchdog()
except KeyboardInterrupt:
pool.shutdown()
exit(0)
def handle(self, *args, **options):
# Detach when asked.
if 'detach' in options and options['detach']:
self.detach(pid_file=options['pid_file'] if 'pid_file' in options and options['pid_file'] else 'pyplanet.pid')
# Initiate the logger.
threading.current_thread().setName('Main')
initiate_logger()
logger = logging.getLogger(__name__)
# Initiate the settings by accessing one.
logger.debug('Initiated configuration and environment... (debug on, means no error reporting and verbose output')
if not settings.DEBUG:
logger.info('Initiated configuration and environment...')
logger.info('-------------------------------[ PyPlanet v{} ]-------------------------------'.format(self.get_version()))
# Start god process (the current started process).
self.pool = EnvironmentPool(settings.POOLS, max_restarts=options['max_restarts'], options=options)
self.pool.populate()
# Starting all processes.
self.pool.start()
# Start the watchdog.
try:
self.pool.watchdog()
except KeyboardInterrupt:
self.pool.shutdown()
exit(0)
def get_tempdir():
""" Get the temporary directory where pyelastix stores its temporary
files. The directory is specific to the current process and the
calling thread. Generally, the user does not need this; directories
are automatically cleaned up. Though Elastix log files are also
written here.
"""
tempdir = os.path.join(tempfile.gettempdir(), 'pyelastix')
# Make sure it exists
if not os.path.isdir(tempdir):
os.makedirs(tempdir)
# Clean up all directories for which the process no longer exists
for fname in os.listdir(tempdir):
dirName = os.path.join(tempdir, fname)
# Check if is right kind of dir
if not (os.path.isdir(dirName) and fname.startswith('id_')):
continue
# Get pid and check if its running
try:
pid = int(fname.split('_')[1])
except Exception:
continue
if not _is_pid_running(pid):
_clear_dir(dirName)
# Select dir that included process and thread id
tid = id(threading.current_thread() if hasattr(threading, 'current_thread')
else threading.currentThread())
dir = os.path.join(tempdir, 'id_%i_%i' % (os.getpid(), tid))
if not os.path.isdir(dir):
os.mkdir(dir)
return dir
def __init__(self, thread_nb, board, imageboard, output_folder, folder, is_quiet, condition, check_duplicate):
# Getting info about the imageboard URL
ib_info = imageboard_info.imageboard_info(imageboard)
base_url = ib_info.base_url
image_url = ib_info.image_base_url
thread_subfolder = ib_info.thread_subfolder
image_subfolder = ib_info.image_subfolder
# These URL are the url of the thread
# and the base url where images are stored on the imageboard
self.thread_url = "{0}{1}{2}{3}.json".format(base_url, board, thread_subfolder, thread_nb)
self.image_url = "{0}{1}{2}".format(image_url, board, image_subfolder)
self.tmp_dir = "/tmp/{0}/".format(os.getpid())
self.curr_time = time.strftime('%d%m%Y-%H%M%S')
self.pid = os.getpid()
self.thread = threading.current_thread().name
self.downloaded_log = "{0}/{1}4scanner_dld-{2}-{3}".format(self.tmp_dir, self.curr_time, self.pid, self.thread)
self.out_dir = os.path.join(output_folder, 'downloads', imageboard, board, folder, str(thread_nb))
self.thread_nb = thread_nb
self.imageboard = imageboard
self.board = board
self.condition = condition
self.check_duplicate = check_duplicate
self.is_quiet = is_quiet
# Creating the tmp and output directory
self.create_dir(self.tmp_dir)
self.create_dir(self.out_dir)
# Main download function