def start(address): global _running if _running: raise RuntimeError('trying to start reporter while running') logging.info("Starting hawkular reporter") concurrent.thread(_run, name='hawkular', args=(address,)).start() _running = True
def start(address): global _running if _running: raise RuntimeError('trying to start reporter while running') logging.info("Starting hawkular reporter") concurrent.thread(_run, name='hawkular', args=(address, )).start() _running = True
def server_thread(): def server_proc(sock, callback): conn, addr = sock.accept() callback(conn, addr) server = None sock = None thread = None try: server = http.Server(None, logger) server.start() sock = socket.socket() sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.settimeout(5) sock.bind(("127.0.0.1", 0)) sock.listen(5) thread = concurrent.thread(server_proc, ( sock, server.add_socket, ), log=logger) thread.start() yield sock.getsockname() finally: if thread is not None: thread.join() if sock is not None: sock.close() if server is not None: server.stop()
def run_with_vars(context, task, func, *args, **kwargs): """ Run func in another thread with optional context and task set in the vars thread local. Return the function result or raises the original exceptions raised by func. """ result = [None] def run(): if context: vars.context = context if task: vars.task = task try: result[0] = (True, func(*args, **kwargs)) except: result[0] = (False, sys.exc_info()) t = concurrent.thread(run) t.start() t.join() ok, value = result[0] if not ok: six.reraise(*value) return value
def __init__(self, sdUUID, hostId, interval, changeEvent, checker): self.thread = concurrent.thread(self._run, log=log, name="monitor/" + sdUUID[:7]) self.stopEvent = threading.Event() self.domain = None self.sdUUID = sdUUID self.hostId = hostId self.interval = interval self.changeEvent = changeEvent self.checker = checker self.lock = threading.Lock() self.monitoringPath = None # For backward compatibility, we must present a fake status before # collecting the first sample. The fake status is marked as # actual=False so engine can handle it correctly. self.status = Status(PathStatus(actual=False), DomainStatus(actual=False)) self.isIsoDomain = None self.isoPrefix = None self.lastRefresh = time.time() # Use float to allow short refresh internal during tests. self.refreshTime = \ config.getfloat("irs", "repo_stats_cache_refresh_timeout") self.wasShutdown = False # Used for synchronizing during the tests self.cycleCallback = _NULL_CALLBACK
def test_echo(self, concurrency): msg = b"ping" sockets = [] try: for i in range(concurrency): sock1, sock2 = socket.socketpair() self.loop.create_dispatcher(Echo, sock2) sockets.append(sock1) t = concurrent.thread(self.loop.run_forever) t.start() try: start = time.time() for sock in sockets: osutils.uninterruptible(sock.send, msg) for sock in sockets: data = osutils.uninterruptible(sock.recv, len(msg)) self.assertEqual(data, msg) elapsed = time.time() - start print("%7d echos: %f seconds" % (concurrency, elapsed)) finally: self.loop.call_soon_threadsafe(self.loop.stop) t.join() finally: for sock in sockets: sock.close()
def test_invalidate_wake_up_waiters(self): count = 3 event = concurrent.ValidatingEvent() ready = concurrent.Barrier(count + 1) invalidated = [False] * count def wait(n): ready.wait(1) try: event.wait(1) except concurrent.InvalidEvent: invalidated[n] = True threads = [] try: for i in range(count): t = concurrent.thread(wait, args=(i,)) t.start() threads.append(t) # Wait until all threads entered the barrier. ready.wait(1) # Give threads time to enter the event. time.sleep(0.5) event.valid = False finally: for t in threads: t.join() self.assertTrue(all(invalidated), "Some threads were no invalidated: %s" % invalidated) self.assertFalse(event.valid, "Event is valid")
def test_non_daemon_thread(self): t = concurrent.thread(lambda: None, daemon=False) t.start() try: self.assertFalse(t.daemon) finally: t.join()
def test_default_daemon_thread(self): t = concurrent.thread(lambda: None) t.start() try: self.assertTrue(t.daemon) finally: t.join()
def start(self, blocking): if blocking: return self._dhclient() else: t = concurrent.thread(self._dhclient, name='dhclient/%s' % self.iface) t.start()
def __init__(self, inbox, outbox, hostID, queue, monitorInterval): # Save arguments tpSize = config.getint('irs', 'thread_pool_size') // 2 waitTimeout = wait_timeout(monitorInterval) maxTasks = config.getint('irs', 'max_tasks') self.tp = ThreadPool("mailbox-hsm", tpSize, waitTimeout, maxTasks) self._stop = False self._queue = queue self._activeMessages = {} self._monitorInterval = monitorInterval self._hostID = int(hostID) self._used_slots_array = [0] * MESSAGES_PER_MAILBOX self._outgoingMail = EMPTYMAILBOX self._incomingMail = EMPTYMAILBOX # TODO: add support for multiple paths (multiple mailboxes) self._inCmd = [ constants.EXT_DD, 'if=' + str(inbox), 'iflag=direct,fullblock', 'bs=' + str(MAILBOX_SIZE), 'count=1', 'skip=' + str(self._hostID) ] self._outCmd = [ constants.EXT_DD, 'of=' + str(outbox), 'iflag=fullblock', 'oflag=direct', 'conv=notrunc', 'bs=' + str(MAILBOX_SIZE), 'count=1', 'seek=' + str(self._hostID) ] self._init = False self._initMailbox() # Read initial mailbox state self._msgCounter = 0 self._sendMail() # Clear outgoing mailbox self._thread = concurrent.thread(self._run, name="mailbox-hsm", log=self.log) self._thread.start()
def __init__(self, vm, job, drive): self.vm = vm self.job = job self.drive = drive self.doPivot = job.pivot self._state = self.TRYING self._thread = concurrent.thread(self.run, name="merge/" + job.id[:8])
def __init__( self, c_callback_function, groups=frozenset(), timeout=None, silent_timeout=False, ): self._c_callback_function = c_callback_function self._time_start = None self._timeout = timeout self._silent_timeout = silent_timeout if groups: unknown_groups = frozenset(groups).difference( frozenset(libnl.GROUPS) ) if unknown_groups: raise AttributeError('Invalid groups: %s' % (unknown_groups,)) self._groups = groups else: self._groups = frozenset(libnl.GROUPS.keys()) self._queue = queue.Queue() self._scan_thread = concurrent.thread( self._scan, name="netlink/events" ) self._scanning_started = threading.Event() self._scanning_stopped = threading.Event()
def test_set_wake_up_waiters(self): count = 3 event = concurrent.ValidatingEvent() ready = concurrent.Barrier(count + 1) woke_up = [False] * count def wait(n): ready.wait(1) woke_up[n] = event.wait(1) threads = [] try: for i in range(count): t = concurrent.thread(wait, args=(i,)) t.start() threads.append(t) # Wait until all threads entered the barrier. ready.wait(1) # Give threads time to enter the event. time.sleep(0.5) event.set() finally: for t in threads: t.join() self.assertTrue(all(woke_up), "Some threads did not wake up: %s" % woke_up) self.assertTrue(event.valid, "Event is invalid")
def test_acquire_wait_until_host_id_is_acquired(fake_sanlock, lock): # Starts async host id acquire... lock.acquireHostId(HOST_ID, wait=False) def monitor(): # Simulate the domain monitor checking if host id was acquire every 10 # seconds... for i in range(3): lock.hasHostId(HOST_ID) time.sleep(0.3) fake_sanlock.complete_async(LS_NAME) # Discover that host id was acquired, and wake up threads waiting on # acquire(). lock.hasHostId(HOST_ID) t = concurrent.thread(monitor) t.start() try: # Acquire should wait until host id acquire is completed. lock.acquire(HOST_ID, LEASE) res = fake_sanlock.read_resource(LEASE.path, LEASE.offset) finally: t.join() assert res["acquired"]
def test_acquire_after_inq_lockspace_failure(fake_sanlock, lock): # Starts async host id acquire... lock.acquireHostId(HOST_ID, wait=False) def monitor(): time.sleep(0.3) # Simulate failing hasHostId... fake_sanlock.errors["inq_lockspace"] = fake_sanlock.SanlockException(1) try: lock.hasHostId(HOST_ID) except fake_sanlock.SanlockException: pass time.sleep(0.3) # Make the next try successful fake_sanlock.complete_async(LS_NAME) del fake_sanlock.errors["inq_lockspace"] lock.hasHostId(HOST_ID) t = concurrent.thread(monitor) t.start() try: # Acquire should wait until host id acquire is completed. lock.acquire(HOST_ID, LEASE) res = fake_sanlock.read_resource(LEASE.path, LEASE.offset) finally: t.join() assert res["acquired"]
def test_get_running_trace(self): ready = threading.Event() done = threading.Event() def worker(): inner() def inner(): ready.set() done.wait() t = concurrent.thread(worker, name="Test") t.start() try: if not ready.wait(1): raise RuntimeError("Timeout waiting for worker thread") formatted_traceback = concurrent.format_traceback(t.ident) finally: done.set() t.join() # The functions called from the worker thread should appear in the # traceback. self.assertIn("in worker", formatted_traceback) self.assertIn("in inner", formatted_traceback)
def test_set_wake_up_waiters(self): count = 3 event = concurrent.ValidatingEvent() ready = concurrent.Barrier(count + 1) woke_up = [False] * count def wait(n): ready.wait(1) woke_up[n] = event.wait(1) threads = [] try: for i in range(count): t = concurrent.thread(wait, args=(i, )) t.start() threads.append(t) # Wait until all threads entered the barrier. ready.wait(1) # Give threads time to enter the event. time.sleep(0.5) event.set() finally: for t in threads: t.join() self.assertTrue(all(woke_up), "Some threads did not wake up: %s" % woke_up) self.assertTrue(event.valid, "Event is invalid")
def test_invalidate_wake_up_waiters(self): count = 3 event = concurrent.ValidatingEvent() ready = concurrent.Barrier(count + 1) invalidated = [False] * count def wait(n): ready.wait(1) try: event.wait(1) except concurrent.InvalidEvent: invalidated[n] = True threads = [] try: for i in range(count): t = concurrent.thread(wait, args=(i, )) t.start() threads.append(t) # Wait until all threads entered the barrier. ready.wait(1) # Give threads time to enter the event. time.sleep(0.5) event.valid = False finally: for t in threads: t.join() self.assertTrue(all(invalidated), "Some threads were no invalidated: %s" % invalidated) self.assertFalse(event.valid, "Event is valid")
def test_handle_error_failures(self): class EvilDispatcher(Echo): def handle_read(self): Echo.handle_read(self) raise Exception("Expected error") def handle_error(self): # This is a very big anti-pattern for dispatchers, # asyncore.poll2 will raise errors raised from handle_error. raise Exception("Evil error") def pinger(sock): msg = b"ping" osutils.uninterruptible(sock.send, msg) osutils.uninterruptible(sock.recv, len(msg)) sock.close() self.loop.call_soon_threadsafe(self.loop.stop) sock1, sock2 = socket.socketpair() # The dispatcher and pinger owns the sockets self.loop.create_dispatcher(EvilDispatcher, sock2) t = concurrent.thread(pinger, args=(sock1, )) t.start() try: # Correct error handling willl allow this test to complete without # errors. This used to abort the event loop with the error raised # in handle_error. self.loop.run_forever() finally: t.join()
def test_handle_error_failures(self): class EvilDispatcher(Echo): def handle_read(self): Echo.handle_read(self) raise Exception("Expected error") def handle_error(self): # This is a very big anti-pattern for dispatchers, # asyncore.poll2 will raise errors raised from handle_error. raise Exception("Evil error") def pinger(sock): msg = b"ping" osutils.uninterruptible(sock.send, msg) osutils.uninterruptible(sock.recv, len(msg)) sock.close() self.loop.call_soon_threadsafe(self.loop.stop) sock1, sock2 = socket.socketpair() # The dispatcher and pinger owns the sockets self.loop.create_dispatcher(EvilDispatcher, sock2) t = concurrent.thread(pinger, args=(sock1,)) t.start() try: # Correct error handling willl allow this test to complete without # errors. This used to abort the event loop with the error raised # in handle_error. self.loop.run_forever() finally: t.join()
def start(self): assert not self.run self.__thread = concurrent.thread(self.__run, name="libvirt/events", log=log) self.run = True self.__thread.start()
def __init__(self, poolID, maxHostID, inbox, outbox, monitorInterval=2): """ Note: inbox paramerter here should point to the HSM's outbox mailbox file, and vice versa. """ self._messageTypes = {} # Save arguments self._stop = False self._stopped = False self._poolID = poolID tpSize = config.getint('irs', 'thread_pool_size') / 2 waitTimeout = wait_timeout(monitorInterval) maxTasks = config.getint('irs', 'max_tasks') self.tp = ThreadPool("mailbox-spm", tpSize, waitTimeout, maxTasks) self._inbox = inbox if not os.path.exists(self._inbox): self.log.error("SPM_MailMonitor create failed - inbox %s does not " "exist" % repr(self._inbox)) raise RuntimeError("SPM_MailMonitor create failed - inbox %s does " "not exist" % repr(self._inbox)) self._outbox = outbox if not os.path.exists(self._outbox): self.log.error("SPM_MailMonitor create failed - outbox %s does " "not exist" % repr(self._outbox)) raise RuntimeError("SPM_MailMonitor create failed - outbox %s " "does not exist" % repr(self._outbox)) self._numHosts = int(maxHostID) self._outMailLen = MAILBOX_SIZE * self._numHosts self._monitorInterval = monitorInterval # TODO: add support for multiple paths (multiple mailboxes) self._outgoingMail = self._outMailLen * "\0" self._incomingMail = self._outgoingMail self._inCmd = ['dd', 'if=' + str(self._inbox), 'iflag=direct,fullblock', 'count=1' ] self._outCmd = ['dd', 'of=' + str(self._outbox), 'oflag=direct', 'iflag=fullblock', 'conv=notrunc', 'count=1' ] self._outLock = threading.Lock() self._inLock = threading.Lock() # Clear outgoing mail self.log.debug("SPM_MailMonitor - clearing outgoing mail, command is: " "%s", self._outCmd) cmd = self._outCmd + ['bs=' + str(self._outMailLen)] (rc, out, err) = _mboxExecCmd(cmd, data=self._outgoingMail) if rc: self.log.warning("SPM_MailMonitor couldn't clear outgoing mail, " "dd failed") self._thread = concurrent.thread( self._run, name="mailbox-spm", log=self.log) self._thread.start() self.log.debug('SPM_MailMonitor created for pool %s' % self._poolID)
def handle_request(self): sock, addr = self.queue.get() if sock is self._STOP: return self.log.info("Starting request handler for %s:%d", addr[0], addr[1]) t = concurrent.thread(self._process_requests, args=(sock, addr), log=self.log) t.start()
def __init__(self, poolID, maxHostID, inbox, outbox, monitorInterval=2): """ Note: inbox paramerter here should point to the HSM's outbox mailbox file, and vice versa. """ self._messageTypes = {} # Save arguments self._stop = False self._stopped = False self._poolID = poolID tpSize = config.getint('irs', 'thread_pool_size') // 2 waitTimeout = wait_timeout(monitorInterval) maxTasks = config.getint('irs', 'max_tasks') self.tp = ThreadPool("mailbox-spm", tpSize, waitTimeout, maxTasks) self._inbox = inbox if not os.path.exists(self._inbox): self.log.error("SPM_MailMonitor create failed - inbox %s does not " "exist" % repr(self._inbox)) raise RuntimeError("SPM_MailMonitor create failed - inbox %s does " "not exist" % repr(self._inbox)) self._outbox = outbox if not os.path.exists(self._outbox): self.log.error("SPM_MailMonitor create failed - outbox %s does " "not exist" % repr(self._outbox)) raise RuntimeError("SPM_MailMonitor create failed - outbox %s " "does not exist" % repr(self._outbox)) self._numHosts = int(maxHostID) self._outMailLen = MAILBOX_SIZE * self._numHosts self._monitorInterval = monitorInterval # TODO: add support for multiple paths (multiple mailboxes) self._outgoingMail = self._outMailLen * b"\0" self._incomingMail = self._outgoingMail self._inCmd = ['dd', 'if=' + str(self._inbox), 'iflag=direct,fullblock', 'count=1' ] self._outCmd = ['dd', 'of=' + str(self._outbox), 'oflag=direct', 'iflag=fullblock', 'conv=notrunc', 'count=1' ] self._outLock = threading.Lock() self._inLock = threading.Lock() # Clear outgoing mail self.log.debug("SPM_MailMonitor - clearing outgoing mail, command is: " "%s", self._outCmd) cmd = self._outCmd + ['bs=' + str(self._outMailLen)] (rc, out, err) = _mboxExecCmd(cmd, data=self._outgoingMail) if rc: self.log.warning("SPM_MailMonitor couldn't clear outgoing mail, " "dd failed") self._thread = concurrent.thread( self._run, name="mailbox-spm", log=self.log) self.log.debug('SPM_MailMonitor created for pool %s' % self._poolID)
def __init__(self, vm, dst='', dstparams='', mode=MODE_REMOTE, method=METHOD_ONLINE, tunneled=False, dstqemu='', abortOnError=False, consoleAddress=None, compressed=False, autoConverge=False, recovery=False, **kwargs): self.log = vm.log self._vm = vm self._dst = dst self._mode = mode if method != METHOD_ONLINE: self.log.warning( 'migration method %s is deprecated, forced to "online"', method) self._dstparams = dstparams self._enableGuestEvents = kwargs.get('enableGuestEvents', False) self._machineParams = {} # TODO: conv.tobool shouldn't be used in this constructor, the # conversions should be handled properly in the API layer self._tunneled = conv.tobool(tunneled) self._abortOnError = conv.tobool(abortOnError) self._consoleAddress = consoleAddress self._dstqemu = dstqemu self._downtime = kwargs.get('downtime') or \ config.get('vars', 'migration_downtime') self._maxBandwidth = int( kwargs.get('maxBandwidth') or config.getint('vars', 'migration_max_bandwidth') ) self._autoConverge = conv.tobool(autoConverge) self._compressed = conv.tobool(compressed) self._incomingLimit = kwargs.get('incomingLimit') self._outgoingLimit = kwargs.get('outgoingLimit') self.status = { 'status': { 'code': 0, 'message': 'Migration in progress'}} # we need to guard against concurrent updates only self._lock = threading.Lock() self._progress = 0 self._thread = concurrent.thread( self.run, name='migsrc/' + self._vm.id[:8]) self._preparingMigrationEvt = True self._migrationCanceledEvt = threading.Event() self._monitorThread = None self._destServer = None self._convergence_schedule = { 'init': [], 'stalling': [] } self._use_convergence_schedule = False if 'convergenceSchedule' in kwargs: self._convergence_schedule = kwargs.get('convergenceSchedule') self._use_convergence_schedule = True self.log.debug('convergence schedule set to: %s', str(self._convergence_schedule)) self._started = False self._failed = False self._recovery = recovery
def progress(op, estimated_size): done = threading.Event() th = concurrent.thread(volume_progress, args=(op, done, estimated_size)) th.start() try: yield th finally: done.set() th.join()
def __init__(self, vm, startTime, conv_schedule): super(MonitorThread, self).__init__() self._stop = threading.Event() self._vm = vm self._startTime = startTime self.daemon = True self.progress = None self._conv_schedule = conv_schedule self._thread = concurrent.thread(self.run, name='migmon/' + self._vm.id[:8])
def test_run_callable_in_thread(self): self.thread = threading.current_thread() def run(): self.thread = threading.current_thread() t = concurrent.thread(run) t.start() t.join() self.assertEqual(t, self.thread)
def test_pass_args(self): self.args = () def run(*args): self.args = args t = concurrent.thread(run, args=(1, 2, 3)) t.start() t.join() self.assertEqual((1, 2, 3), self.args)
def test_pass_kwargs(self): self.kwargs = () def run(**kwargs): self.kwargs = kwargs kwargs = {'a': 1, 'b': 2} t = concurrent.thread(run, kwargs=kwargs) t.start() t.join() self.assertEqual(kwargs, self.kwargs)
def __init__(self, executor, scheduler, name, log=None): self._executor = executor self._scheduler = scheduler self._discarded = False self._task_counter = 0 self._lock = threading.Lock() if log is not None: self._log = log self._thread = concurrent.thread(self._run, name=name, log=self._log) self._task = None self._scheduled_check = None
def __init__(self, interval=10): self._lock = threading.Lock() self._status = {} self._thread = None self._done = threading.Event() self._interval = interval self._thread = concurrent.thread(self._run, name="mpathhealth", log=log) # Used for synchronization during testing self.callback = _NULL_CALLBACK
def test_inq_lockspace_acquiring_wait(self): fs = FakeSanlock() fs.add_lockspace("lockspace", 1, "path", async=True) t = concurrent.thread(fs.complete_async, args=("lockspace", )) t.start() try: acquired = fs.inq_lockspace("lockspace", 1, "path", wait=True) finally: t.join() self.assertTrue(acquired, "lockspace not acquired")
def test_inq_lockspace_acquiring_wait(self): fs = FakeSanlock() fs.add_lockspace("lockspace", 1, "path", async=True) t = concurrent.thread(fs.complete_async, args=("lockspace",)) t.start() try: acquired = fs.inq_lockspace("lockspace", 1, "path", wait=True) finally: t.join() self.assertTrue(acquired, "lockspace not acquired")
def __init__(self, log): self.log = log self._quit = False self._epoll = select.epoll() self._channels = {} self._unconnected = {} self._update_lock = threading.Lock() self._add_channels = {} self._del_channels = [] self._timeout = None self._thread = concurrent.thread(self.run, name='vmchannels')
def __init__(self, vm, startTime, conv_schedule, use_conv_schedule): super(MonitorThread, self).__init__() self._stop = threading.Event() self._vm = vm self._startTime = startTime self.daemon = True self.progress = None self._conv_schedule = conv_schedule self._use_conv_schedule = use_conv_schedule self.downtime_thread = _FakeThreadInterface() self._thread = concurrent.thread( self.run, name='migmon/' + self._vm.id[:8])
def __init__(self, log): self.log = log self._quit = False self._epoll = select.epoll() self._channels = {} self._unconnected = {} self._update_lock = threading.Lock() self._add_channels = {} self._del_channels = [] self._timeout = None self._thread = concurrent.thread( self.run, name='vmchannels' )
def test_inq_lockspace_releasing_wait(self): fs = FakeSanlock() fs.write_lockspace("lockspace", "path") fs.add_lockspace("lockspace", 1, "path") fs.rem_lockspace("lockspace", 1, "path", **{'async': True}) t = concurrent.thread(fs.complete_async, args=("lockspace",)) t.start() try: acquired = fs.inq_lockspace("lockspace", 1, "path", wait=True) finally: t.join() self.assertFalse(acquired, "lockspace not released")
def itmap(func, iterable, maxthreads=UNLIMITED_THREADS): """ Make an iterator that computes the function using arguments from the iterable. It works similar to tmap by running each operation in a different thread, this causes the results not to return in any particular order so it's good if you don't care about the order of the results. maxthreads stands for maximum threads that we can initiate simultaneosly. If we reached to max threads the function waits for thread to finish before initiate the next one. """ if maxthreads < 1 and maxthreads != UNLIMITED_THREADS: raise ValueError("Wrong input to function itmap: %s", maxthreads) respQueue = queue.Queue() def wrapper(value): try: respQueue.put(func(value)) except Exception as e: respQueue.put(e) threadsCreated = 0 threadsCount = 0 for arg in iterable: if maxthreads != UNLIMITED_THREADS: if maxthreads == 0: # This not supposed to happened. If it does, it's a bug. # maxthreads should get to 0 only after threadsCount is # greater than 1 if threadsCount < 1: raise RuntimeError("No thread initiated") else: yield respQueue.get() # if yield returns one thread stopped, so we can run # another thread in queue maxthreads += 1 threadsCount -= 1 name = "itmap/%d" % threadsCreated t = concurrent.thread(wrapper, args=(arg,), name=name) t.start() threadsCreated += 1 threadsCount += 1 maxthreads -= 1 # waiting for rest threads to end for i in range(threadsCount): yield respQueue.get()
def test_close(self): reactor = Reactor() thread = concurrent.thread(reactor.process_requests, name='test ractor') thread.start() s1, s2 = socket.socketpair() with closing(s2): disp = reactor.create_dispatcher(s1, impl=TestingImpl()) reactor.stop() thread.join(timeout=1) self.assertTrue(disp.closing) self.assertFalse(reactor._wakeupEvent.closing)
def __init__(self, name="Scheduler", clock=time.time): """ Initialize a scheduler. Arguments: name Used as sheculer thread name clock Callable returning current time (defualt time.time) """ self._name = name self._clock = clock self._cond = threading.Condition(threading.Lock()) self._running = False self._calls = [] self._thread = concurrent.thread(self._run, name=self._name, log=self._log)
def test_pass_args_and_kwargs(self): self.args = () self.kwargs = {} def run(*args, **kwargs): self.args = args self.kwargs = kwargs args = (1, 2) kwargs = {'a': 3, 'b': 4} t = concurrent.thread(run, args=args, kwargs=kwargs) t.start() t.join() self.assertEqual(args, self.args) self.assertEqual(kwargs, self.kwargs)
def __del__(self): def finalize(log, owner, taskDir): log.warn("Task was autocleaned") owner.releaseAll() if taskDir is not None: getProcPool().fileUtils.cleanupdir(taskDir) if not self.state.isDone(): taskDir = None if (self.cleanPolicy == TaskCleanType.auto and self.store is not None): taskDir = os.path.join(self.store, self.id) t = concurrent.thread( finalize, args=(self.log, self.resOwner, taskDir), name="task/" + self.id[:8]) t.start()
def __init__(self, groups=frozenset(), timeout=None, silent_timeout=False): self._time_start = None self._timeout = timeout self._silent_timeout = silent_timeout if groups: unknown_groups = frozenset(groups).difference( frozenset(libnl.GROUPS)) if unknown_groups: raise AttributeError('Invalid groups: %s' % (unknown_groups,)) self._groups = groups else: self._groups = frozenset(libnl.GROUPS.keys()) self._queue = queue.Queue() self._scan_thread = concurrent.thread(self._scan, name="netlink/events") self._scanning_started = threading.Event() self._scanning_stopped = threading.Event()