Example #1
0
    def threadManagerRun(self):
        #Wait for the main thread to shut down
        while(threading.main_thread().is_alive()):
            time.sleep(0)
        threading.main_thread().join()
        self.closed = True

        #Shutdown external communications
        if (self.serialTalker):
            self.serialTalker.shutDown()
        #DEBUG: Confirm shutdown control returns to main controller
        print("JINX Controller has shut down serial talker")

        if (self.serverTalker):
            self.serverTalker.shutDown()
        #DEBUG: Confirm shutdown control returns to main controller
        print("JINX Controller has shut down Server")

        #DEBUG: Waiting for threads to close
        print("Waiting for serial talker and server to shut down.")
        if (self.serialThread.is_alive()):
            self.serialThread.join()
        if (self.serverThread.is_alive()):
            self.serverThread.join()

        #DEBUG: Confirm all stopped
        print("All from JINX stopped")
Example #2
0
    def pull_in_the_background(self):
        """
        Keep pulling data in the background.

        TODO:
        - Stop pulling when running out of memory. Perhaps start deleting rows
          that are not in the region of interest?
        - Continue to pull data outside the region_of_interest when we got
          all of that?

        """
        if (not self.stop_pulling) and threading.main_thread().is_alive():
            self.pull_region_of_interest()
            if (not self.stop_pulling) and threading.main_thread().is_alive():
                threading.Timer(10, self.pull_in_the_background).start()
Example #3
0
    def handle(self):
        # Flush any pending changes. Any updates
        # beyond here will be field specific
        self.save()

        is_main_thread = threading.current_thread() == threading.main_thread()

        if is_main_thread:
            # Protect ourselves from SIGTERM
            def on_sigterm(sig, frame):
                self.cancel()
            prev_handler = signal.signal(signal.SIGTERM, on_sigterm)

        self.start()
        try:
            yield
        except Retry as e:
            self.fail(e)
            self.reschedule(claim=True)
        except Exception as e:
            self.fail(e)
            raise
        else:
            # If the handler didn't handle setting a status, assume success
            if self.status == self.STATUS.started:
                self.succeed()
        finally:
            if is_main_thread:
                # Detach from SIGTERM, resetting the previous handle
                signal.signal(signal.SIGTERM, prev_handler)
Example #4
0
def asynchronous(func: Callable[..., Any]):
    """
    Wraps a callable so that it is guaranteed to be called in the event loop.
    If it returns a coroutine or a Future and the call came from another thread, the coroutine
    or Future is first resolved before returning the result to the caller.
    """

    @wraps(func, updated=())
    def wrapper(*args, **kwargs):
        @coroutine
        def callback():
            try:
                retval = func(*args, **kwargs)
                if iscoroutine(retval) or isinstance(retval, Future):
                    retval = yield from retval
            except Exception as e:
                f.set_exception(e)
            except BaseException as e:  # pragma: no cover
                f.set_exception(e)
                raise
            else:
                f.set_result(retval)

        if current_thread() is event_loop_thread:
            return func(*args, **kwargs)
        else:
            f = Future()
            event_loop.call_soon_threadsafe(async, callback())
            return f.result()

    event_loop = get_event_loop()
    event_loop_thread = main_thread()
    return wrapper
Example #5
0
	def mainloop(self):
		''' Starts the endless game loop '''
		self.ping = time.time() + self.PING_INTERVAL

		while True:
			pkt = self.receive(blocking=False)
			self.send()

			# Check if brain is alive
			if not threading.main_thread().is_alive():
				self.log.info("Brain died, terminating")
				break

			# Send ping if needed
			if self.lc and self.ping < time.time():
				po = packets.PingPacket()
				po.fill(0)
				self.queue(po)
				self.ping = time.time() + self.PING_INTERVAL

			# Process packet
			if pkt is None:
				time.sleep(0.01)
			else:
				self.handlePacket(pkt)
Example #6
0
 def bind(self):
     assert threading.current_thread() is threading.main_thread()
     length = None
     first_atname = None
     if self.instanced:
         instances = None
         instanced_first_atname = None
     vao = gl.glGenVertexArrays(1)
     gl.glBindVertexArray(vao)
     if self.indices:
         self.indices.bind()
     for atname, at in self.attributes.items():
         at.bind()
         if at.instanced:
             if instances is None:
                 instances = at.length
                 instanced_first_atname = atname
             if instances != at.length:
                 raise ValueError((instanced_first_atname, instances), (atname, at.length))
         else:
             if length is None:
                 length = at.length
                 first_atname = atname
             if length != at.length:
                 raise ValueError((first_atname, length), (atname, at.length))
     if length is None:
         length = 0
     if self.instanced:
         self.instances = instances
     self.length = length
     self.vao = vao
     self.dirty = False
    async def _aenter(self, daemon=False):
        async with AsyncExitStack() as enter_stack:
            if daemon and self._open_count == 0:
                raise RuntimeError("The client is not active, first use of the client must not be daemon")
            if self._shutdown:
                raise RuntimeError("Cannot reuse a client after it was once shut down")

            logger.debug("Entering as %s", "daemon" if daemon else "regular")

            self._open_count += 1

            @enter_stack.callback
            def decrement_open_count():
                self._open_count -= 1

            if daemon:
                self._daemon_count += 1

                # testing: I see no good way to cause a fault that triggers this...
                # the code is almost the same as decrement_open_count, so ignore it for coverage
                @enter_stack.callback
                def decrement_daemon_count():
                    self._daemon_count -= 1  # pragma: nocover

            if self._open_count == 1:
                logger.debug("Activating client...")

                async with AsyncExitStack() as stack:
                    @asynccontextmanager
                    async def start() -> Component[None]:
                        component = await start_component(self.workload)
                        try:
                            yield component
                        finally:
                            await component.stop()
                    await stack.enter_async_context(start())

                    if threading.current_thread() is threading.main_thread():
                        loop = asyncio.get_event_loop()

                        def sigint_handler():
                            task = loop.create_task(self.shutdown())

                            # if this signal handler is called during _aenter, register the await with `stack`;
                            # otherwise, with `self._exit_stack`
                            exit_stack = self._exit_stack if self._exit_stack is not None else stack

                            @exit_stack.push_async_callback
                            async def await_shutdown():
                                await task

                        stack.enter_context(shutdown_handler.register_async(signal.SIGINT, sigint_handler))

                    # save the exit actions that need undoing...
                    self._exit_stack = stack.pop_all()

            # ...and discard those that were only for the error case
            enter_stack.pop_all()
            logger.debug("Open: %d (%d daemon)", self._open_count, self._daemon_count)
            return self
Example #8
0
def remove_heart_log(*args, **kwargs):
    if six.PY2:
        if threading.current_thread().name == 'MainThread':
            debug_log(*args, **kwargs)
    else:
        if threading.current_thread() == threading.main_thread():
            debug_log(*args, **kwargs)
Example #9
0
 def draw(self):
     assert threading.current_thread() is threading.main_thread()
     if self.dirty or not self.vao:
         self.bind()
     assert opengl_current_context() == self.context, "Cannot invoke Renderer from a different OpenGL context"
     gl.glBindVertexArray(self.vao)
     indexed = (self.indices is not None)
     if self.command == "points":
         mode = gl.GL_POINTS
     elif self.command == "lines":
         mode = gl.GL_LINES
     elif self.command == "triangles":
         mode = gl.GL_TRIANGLES
     elif self.command == "triangle_strip":
         mode = gl.GL_TRIANGLE_STRIP
     elif self.command == "triangle_fan":
         mode = gl.GL_TRIANGLE_FAN
     else:
         raise ValueError(self.command)
     if not indexed:
         if not self.instanced:
             gl.glDrawArrays(mode, 0, self.length)
         else:
             gl.glDrawArraysInstanced(mode, 0, self.length, self.instances)
     else:
         if not self.instanced:
             gl.glDrawElements(mode, self.indices.length,
               self.indices.gl_dtype, ctypes.c_void_p(self.indices.offset))
         else:
             gl.glDrawElementsInstanced(mode, self.indices.length,
               self.indices.gl_dtype, ctypes.c_void_p(self.indices.offset),
               self.instances)
Example #10
0
    def _run(self):
        parent = threading.main_thread()

        if self._restarting:
            self._parent._notify_engine_restarted(self)
            self._restarting = False

        while parent.is_alive():
            try:
                bytes = self._socket.recv()
                message = jcoms.ComsMessage()
                message.ParseFromString(bytes)
                self._receive(message)

            except nanomsg.NanoMsgAPIError as e:
                if e.errno != nanomsg.ETIMEDOUT and e.errno != nanomsg.EAGAIN:
                    raise e

            self._process.poll()
            if self._process.returncode is not None:
                if self._restarting is False and self._stopping is False:
                    log.error('Engine process terminated with exit code {}\n'.format(self._process.returncode))
                break

        self._socket.close()
        if self._restarting:
            log.info('Restarting engine')
            self._restarting = False
            self._stopping = False
            self.start()
        else:
            self._stopped = True
            self._parent._notify_engine_event({ 'type': 'terminated' })
Example #11
0
 def bind(self):
     assert threading.current_thread() is threading.main_thread()
     if self.length:
         self.unbind()
     if self.glsl_dtype == "vec4":
         size, dtype = 4, gl.GL_FLOAT
     elif self.glsl_dtype == "vec3":
         size, dtype = 3, gl.GL_FLOAT
     elif self.glsl_dtype == "vec2":
         size, dtype = 2, gl.GL_FLOAT
     elif self.glsl_dtype == "float":
         size, dtype = 1, gl.GL_FLOAT
     else:
         raise TypeError(self.glsl_dtype)
     self.verify_dtype()
     self.store.bind()
     offset = self.store.offset
     stride = self.store.strides[0]
     buf = self.store.opengl_id
     loc = gl.glGetAttribLocation(self.shader_program, self.attribute)
     if loc == -1:
         print("WARNING: unused attribute '%s'" % self.attribute)
         self.enabled = False
     else:
         gl.glEnableVertexAttribArray(loc)
         gl.glBindBuffer(gl.GL_ARRAY_BUFFER, buf)
         gl.glVertexAttribPointer(loc, size, dtype, False, stride, ctypes.c_void_p(offset))
         if self.instanced:
             gl.glVertexAttribDivisor(loc,1)
         self.enabled = True
     self.length = self.store.shape[0]
Example #12
0
def _init_scn():
    """ initialize once and only in mainthread """
    global _is_init_already
    if not _is_init_already and threading.current_thread() == threading.main_thread():
        _is_init_already = True
        logging.basicConfig(level=loglevel_converter(config.default_loglevel), format=config.logformat)
        signal.signal(signal.SIGINT, _signal_handler)
Example #13
0
def is_main_thread():
    """
    Return True if the current thread is the main thread.
    """
    if sys.version_info[0] >= 3:
        return threading.current_thread() == threading.main_thread()
    else:
        return isinstance(threading.current_thread(), threading._MainThread)
Example #14
0
def _timeout_context(seconds, exception):
    """Timeout context manager that works in parent or child thread"""
    if seconds is None or seconds <= 0: # pylint: disable=no-else-return
        return _noop()
    elif threading.current_thread() == threading.main_thread():
        return _timeout_context_main(seconds, exception)
    else:
        return _timeout_context_child(seconds, exception)
Example #15
0
 def __check_bot_id(self, name: str):
     res = re.fullmatch(r'([0-9a-zA-Z\-]+)(\.[0-9]+)?', name)
     if res:
         if not(res.group(2) and threading.current_thread() == threading.main_thread()):
             return name, res.group(1), res.group(2)[1:] if res.group(2) else None
     self.__log_buffer.append(('error',
                               "Invalid bot id, must match '"
                               r"[^0-9a-zA-Z\-]+'."))
     self.stop()
Example #16
0
    def is_main(self, thread=None):
        """Check if the thread is the main thread.

        thread - the thread to check. Use current thread if not provided.
        """
        if not thread:
            thread = self.current()
        with self.lock:
            return thread is self.pool[threading.main_thread()][-1]
Example #17
0
    def pause_all(self):
        if threading.active_count() == 1:
            return
#        ok = QMessageBox.question(self, '注意', '所有正在下载任务将被暂停,您是否继续?', QMessageBox.No|QMessageBox.Yes, QMessageBox.No)
#        if ok == QMessageBox.Yes:
        for t in threading.enumerate():
            if t.name == "downloadLrc" or t.name == "volumeThread" or t == threading.main_thread():
                continue
            t.pause()
Example #18
0
	def run(self):	#this function is called when the bot starts, it does error detection and stuff
		self.thread_connect.start()
		self.thread_handle.start()
		self.thread_detectTimeout.start()
		#self.thread_consInp.start()
		while not self.stop:
			try:
				if self.breakstuff:
					self.breakstuff = False
					raise I_like_trains
				if self.brokenPipe != False or self.pingTimeout != False:
					self.echo("Broken Pipe: {}".format(self.brokenPipe), "warn")
					self.echo("PingTimeout: {}".format(self.pingTimeout), "warn")
					self.echo("exiting...", "warn")
					self.exit()
					self.echo("reconnecting in 10 seconds")
					time.sleep(4)
				if self.stop == True:
					break
				time.sleep(0.42)

			except Exception as e:
				self.echo(traceback.format_exc(), "warn")
				time.sleep(4)
				return

		self.echo("waiting for threads to stop...")

		for t in threading.enumerate():
			try:
				omitted = False
				if t.ident == None:
					omitted = "not started"
				elif t == threading.main_thread():
					omitted = "main thread"
				elif t == threading.current_thread():
					omitted = "current thread"
				elif t.daemon:
					omitted = "daemon"

				if omitted:
					self.echo("omitted {omitted} thread {thread}".format(thread=t.name, omitted=omitted))
				else:
					self.echo("joining thread {thread}".format(thread=t.name))
					t.join(5)
					if t.is_alive():
						self.echo("thread {thread} did not exit within 5 seconds!".format(thread=t.name))
					else:
						self.echo("thread {thread} exited!".format(thread=t.name))
			except:
				self.echo(traceback.format_exc(), "warn")

		self.echo("all threads stopped!")
		self.echo("exiting in 2 seconds")
		time.sleep(2)
		Echo.end(self.screen)
		return
Example #19
0
    def run_with_lock(*args, **kwargs):
        with blivet_lock:
            if current_thread() == main_thread():
                exn_info = get_thread_exception()
                if exn_info[1]:
                    clear_thread_exception()
                    raise ThreadError("raising queued exception") from exn_info[1]

            return m(*args, **kwargs)
Example #20
0
    def new_event_loop(self):
        """Create a new event loop and return it."""
        if not self._default_loop and threading.current_thread() == threading.main_thread():
            loop = self.get_default_loop()
        else:
            loop = CFEventLoop(self._lifecycle)
        loop._policy = self

        return loop
Example #21
0
 def sanity_check(self):
     from PyQt5.QtGui import QOpenGLContext
     context = QOpenGLContext.currentContext()
     assert context
     assert threading.current_thread() is threading.main_thread()
     if self._opengl_context is not None:
         #assert context is self._opengl_context
         if context is not self._opengl_context:
             self.destroy()
             self.create()
Example #22
0
 def destroy(self):
     #print("GLStore DESTROY")
     try:
         assert threading.current_thread() is threading.main_thread()
         if self._id is not None:
             gl.glDeleteBuffers(1, [self._id])
     finally:
         self._id = None
         self._dirty = True
         self._opengl_context = None
Example #23
0
 def destroy(self):
     #print("GLTexStore DESTROY")
     try:
         assert threading.current_thread() is threading.main_thread()
         if self._id is not None:
             gl.glDeleteTextures(self._id)
     finally:
         self._id = None
         self._dirty = set("data")
         self._opengl_context = None
Example #24
0
 def waitForClose(self):
     if self._visible:
         if threading.current_thread() != threading.main_thread():
             self._lock.acquire()
             self._lock.release()
         else:
             # If this is not run from a separate thread, we need to ensure that the events are still processed.
             while self._visible:
                 time.sleep(1 / 50)
                 QCoreApplication.processEvents()  # Ensure that the GUI does not freeze.
Example #25
0
    def main_gui_loop(self):
        print("Running main gui loop in thread", threading.current_thread())

        if threading.current_thread() is not threading.main_thread():
            self.loop = new_event_loop()
            asyncio.set_event_loop(self.loop)

        self._build_interface()
        alimaster.keep_tk_awake(self.root)
        self.root.mainloop()
    def test_main_thread(self):
        current = threading.current_thread()
        self.assertFalse(isinstance(current, threading._DummyThread))
        self.assertTrue(isinstance(current, monkey.get_original('threading', 'Thread')))
        # in 3.4, if the patch is incorrectly done, getting the repr
        # of the thread fails
        repr(current)

        if hasattr(threading, 'main_thread'): # py 3.4
            self.assertEqual(threading.current_thread(), threading.main_thread())
Example #27
0
 def show(self):
     # Emit signal so the right thread actually shows the view.
     if threading.current_thread() != threading.main_thread():
         self._lock.acquire()
     # Reset the result
     self._result = {"machine": self._default_strategy,
                     "quality_changes": self._default_strategy,
                     "material": self._default_strategy}
     self._visible = True
     self.showDialogSignal.emit()
Example #28
0
 def __init__(self, ui):
     self.local_searcher = LocalSearch()
     self.online_searcher = OnlineSearch()
     self.ui = ui
     self.local_list = self.ui.get_object('LocalListBox')
     self.local_list.show()
     self.online_list = self.ui.get_object('OnlineListBox')
     self.search_entry = self.ui.get_object('SearchEntry')
     self.search_entry.connect('search_changed', self.search_changed)
     self.local_searcher.connect('result-found', self.result_found)
     self.search_thread = threading.main_thread()
    def __reportCrash(etype, value, tb):
        sys.__excepthook__(etype, value, tb)

        formatted = "".join(traceback.format_exception(etype, value, tb))

        CrashReport(formatted)

        if threading.current_thread() == threading.main_thread():
            sys.exit(os.EX_SOFTWARE)  # Make sure MainThread exceptions also causes app termination.
        else:
            os._exit(os.EX_SOFTWARE)
Example #30
0
def _is_main_thread():
    """Need to determine if the main thread for setting signals

    Returns:
        bool: if running in the main thread
    """
    if hasattr(threading, 'main_thread'):
        # Python 3
        return threading.current_thread() == threading.main_thread()
    # Python 2: See http://stackoverflow.com/a/23207116
    return threading.current_thread().__class__ == threading._MainThread
Example #31
0
 def _shutdown_adb_server(self):
     while threading.main_thread().is_alive():
         sleep(0.2)
     logger.info('Main thread exited, terminate adb server process')
     spawn_process_raw([self._adb, 'kill-server'])
                    cpt = 0
                    for ligneFile in file:
                        cpt += 1
                        message += ligneFile
                        if len(arg) == 3 : #nblignes spécifié
                            if cpt >= int(arg[1]):
                                break
        socket_client.send(message.encode())




sock_server = socket() #TCP socket
sock_server.bind(("", int(sys.argv[1])))
sock_server.listen(4)
print("le serveur écoute sur le port " + sys.argv[1], file=sys.stderr)
print("son repertoire de base est " + DOC_ROOT, file=sys.stderr)
while True:
    try:
        sock_client , adr_client = sock_server.accept()
        print (f"Connexion de {adr_client}")
        threading.Thread(target=traiter_client , args=(sock_client,)).start()
    except KeyboardInterrupt:
        break
sock_server.shutdown(SHUT_RDWR)
print('\nshutting down')
for t in threading.enumerate():
    if t != threading.main_thread():
        t.join()
sys.exit(0)
    "Yahoo": "http://www.yahoo.com",
}

print("Main thread starting execution...")
for key in test_dict:
    thread = threading.Thread(target=download_url,
                              name=key,
                              args=(key, test_dict[key]),
                              daemon=True)
    print("State of thread Before start {}: {}".format(thread.name,
                                                       repr(thread)))
    threads.append(thread)
    thread.start()
    print("State of thread After start {}: {}".format(thread.name,
                                                      repr(thread)))

print("Main thread continuing execution...")
for thread in threading.enumerate():
    print("Thread name is {}".format(thread.name))
    if thread is threading.main_thread():
        continue

    thread.join()

for thread in threads:
    print("Thread {} is Alive? {}".format(thread.name, thread.isAlive()))
    print("State of thread Before start {}: {}".format(thread.name,
                                                       repr(thread)))

print("Main thread exiting...")
Example #34
0
 def assert_is_on_main_thread(*args, **kwargs):
     self.assertEqual(threading.main_thread(), threading.current_thread())
Example #35
0
 def main_native_thread():
     return __threading__.main_thread() # pylint:disable=no-member
Example #36
0
def showthreadinfo():
    print(
        'current thread = {}\nname thread = {}\nactive count = {}\nenumerate = {}\n'
        .format(threading.current_thread(), threading.main_thread(),
                threading.active_count(), threading.enumerate()))
Example #37
0
    def __enter__(self):
        self._signal_received = False

        if threading.currentThread() is threading.main_thread():
            self._old_handler = signal.signal(signal.SIGINT, self.handler)
Example #38
0
import random
import threading
import time
import logging


def worker():
    """thread worker function"""
    pause = random.randint(1, 5) / 10
    logging.debug("sleeping %0.2f", pause)
    time.sleep(pause)
    logging.debug("ending")


logging.basicConfig(
    level=logging.DEBUG,
    format='(%(threadName)-10s) %(message)s',
)

for i in range(3):
    t = threading.Thread(target=worker, daemon=True)
    t.start()

main_thread = threading.main_thread()
for t in threading.enumerate():
    if t is main_thread:
        continue
    logging.debug("joining %s", t.getName())
    t.join()
Example #39
0
def is_main_thread():
    return threading.current_thread() is threading.main_thread()
Example #40
0
def run_maestral_daemon(config_name='maestral', run=True, log_to_stdout=False):
    """
    Wraps :class:`main.Maestral` as Pyro daemon object, creates a new instance and starts
    Pyro's event loop to listen for requests on a unix domain socket. This call will block
    until the event loop shuts down.

    This command will return silently if the daemon is already running.

    :param str config_name: The name of the Maestral configuration to use.
    :param bool run: If ``True``, start syncing automatically. Defaults to ``True``.
    :param bool log_to_stdout: If ``True``, write logs to stdout. Defaults to ``False``.
    """
    import threading
    from maestral.main import Maestral

    sock_name = sockpath_for_config(config_name)
    pid_name = pidpath_for_config(config_name)

    lockfile = PIDLockFile(pid_name)

    if threading.current_thread() is threading.main_thread():
        signal.signal(signal.SIGTERM, _sigterm_handler)

    # acquire PID lock file

    try:
        lockfile.acquire(timeout=1)
    except (AlreadyLocked, LockTimeout):
        if is_pidfile_stale(lockfile):
            lockfile.break_lock()
        else:
            logger.debug(f'Maestral already running')
            return

    # Nice ourselves give other processes priority. We will likely only
    # have significant CPU usage in case of many concurrent downloads.
    os.nice(10)

    logger.debug(f'Starting Maestral daemon on socket "{sock_name}"')

    try:
        # clean up old socket
        try:
            os.remove(sock_name)
        except FileNotFoundError:
            pass

        daemon = Daemon(unixsocket=sock_name)

        # expose maestral as Pyro server
        # convert selected methods to one way calls so that they don't block
        ExposedMaestral = expose(Maestral)

        ExposedMaestral.stop_sync = oneway(ExposedMaestral.stop_sync)
        ExposedMaestral.pause_sync = oneway(ExposedMaestral.pause_sync)
        ExposedMaestral.shutdown_pyro_daemon = oneway(
            ExposedMaestral.shutdown_pyro_daemon)

        m = ExposedMaestral(config_name, run=run, log_to_stdout=log_to_stdout)

        daemon.register(m, f'maestral.{config_name}')
        daemon.requestLoop(loopCondition=m._loop_condition)
        daemon.close()
    except Exception:
        traceback.print_exc()
    except (KeyboardInterrupt, SystemExit):
        logger.info('Received system exit')
        sys.exit(0)
    finally:
        lockfile.release()
Example #41
0
def check_ml(fid):
    on_ml = threading.current_thread() == threading.main_thread()
    print("%s on mainloop: " % fid, on_ml)
Example #42
0
def 线程_取主线程():
    """
 返回主要Thread对象。在正常情况下,主线程是启动Python解释器的线程。

  """
    return threading.main_thread()
Example #43
0
    def __init__(self,
                 *,
                 address=None,
                 port=51234,
                 persist_file="accessory.state",
                 pincode=None,
                 encoder=None,
                 loader=None,
                 loop=None,
                 mac=None,
                 listen_address=None,
                 advertised_address=None,
                 interface_choice=None,
                 zeroconf_instance=None):
        """
        Initialize a new AccessoryDriver object.

        :param pincode: The pincode that HAP clients must prove they know in order
            to pair with this `Accessory`. Defaults to None, in which case a random
            pincode is generated. The pincode has the format "xxx-xx-xxx", where x is
            a digit.
        :type pincode: bytearray

        :param port: The local port on which the accessory will be accessible.
            In other words, this is the port of the HAPServer.
        :type port: int

        :param address: The local address on which the accessory will be accessible.
            In other words, this is the address of the HAPServer. If not given, the
            driver will try to select an address.
        :type address: str

        :param persist_file: The file name in which the state of the accessory
            will be persisted. This uses `expandvars`, so may contain `~` to
            refer to the user's home directory.
        :type persist_file: str

        :param encoder: The encoder to use when persisting/loading the Accessory state.
        :type encoder: AccessoryEncoder

        :param mac: The MAC address which will be used to identify the accessory.
            If not given, the driver will try to select a MAC address.
        :type mac: str

        :param listen_address: The local address on the HAPServer will listen.
            If not given, the value of the address parameter will be used.
        :type listen_address: str

        :param advertised_address: The address of the HAPServer announced via mDNS.
            This can be used to announce an external address from behind a NAT.
            If not given, the value of the address parameter will be used.
        :type advertised_address: str

        :param interface_choice: The zeroconf interfaces to listen on.
        :type InterfacesType: [InterfaceChoice.Default, InterfaceChoice.All]

        :param zeroconf_instance: A Zeroconf instance. When running multiple accessories or
            bridges a single zeroconf instance can be shared to avoid the overhead
            of processing the same data multiple times.
        """
        if loop is None:
            if sys.platform == "win32":
                loop = asyncio.ProactorEventLoop()
            else:
                loop = asyncio.new_event_loop()

            executor_opts = {"max_workers": None}
            if sys.version_info >= (3, 6):
                executor_opts["thread_name_prefix"] = "SyncWorker"

            self.executor = ThreadPoolExecutor(**executor_opts)
            loop.set_default_executor(self.executor)
            self.tid = threading.current_thread()
        else:
            self.tid = threading.main_thread()
            self.executor = None

        self.loop = loop

        self.accessory = None
        if zeroconf_instance is not None:
            self.advertiser = zeroconf_instance
        elif interface_choice is not None:
            self.advertiser = Zeroconf(interfaces=interface_choice)
        else:
            self.advertiser = Zeroconf()
        self.persist_file = os.path.expanduser(persist_file)
        self.encoder = encoder or AccessoryEncoder()
        self.topics = {}  # topic: set of (address, port) of subscribed clients
        self.loader = loader or Loader()
        self.aio_stop_event = asyncio.Event(loop=loop)
        self.stop_event = threading.Event()

        self.safe_mode = False

        self.mdns_service_info = None
        self.srp_verifier = None

        address = address or util.get_local_address()
        advertised_address = advertised_address or address
        self.state = State(address=advertised_address,
                           mac=mac,
                           pincode=pincode,
                           port=port)

        listen_address = listen_address or address
        network_tuple = (listen_address, self.state.port)
        self.http_server = HAPServer(network_tuple, self)
async def test_call_in_executor(executor):
    """Test that call_in_thread actually runs the target in a worker thread."""
    assert not await call_in_executor(
        lambda: current_thread() is main_thread(), executor=executor)
Example #45
0
 def f():
     self.assertNotEqual(threading.main_thread().ident,
                         threading.current_thread().ident)
Example #46
0
 def warnMsg(message):
     if threading.current_thread() != threading.main_thread():
         return WndUtils.call_in_main_thread(WndUtils.displayMessage, QMessageBox.Warning, message)
     else:
         return WndUtils.displayMessage(QMessageBox.Warning, message)
Example #47
0
from gpsd import NoFixError

try:
    from RPi import GPIO
except ImportError:
    from gpio_stub import GPIO

from aircraft import Aircraft
from gps import GPS
from compass import Compass
from display import Display
from config import Config
from gdl90 import GDL90

threads = {"main": threading.main_thread()}


# Print a stack trace when we receive SIGUSR1
def usr1_handler(sig, frame):
    """Print a stacktrace when we receive SIGUSR1, to enable debugging"""

    print("** SIGUSR1 received - printing tracebacks all threads\n")
    for t_id, t_frame in sys._current_frames().items():
        name = "unknown thread ({})".format(t_id)
        for t_name, t_obj in threads.items():
            if t_obj.ident == t_id:
                name = t_name
                break

        print("=> {}".format(name))
Example #48
0
 def infoMsg(message):
     if threading.current_thread() != threading.main_thread():
         return WndUtils.call_in_main_thread(WndUtils.displayMessage, QMessageBox.Information, message)
     else:
         return WndUtils.displayMessage(QMessageBox.Information, message)
Example #49
0
    def __exit__(self, type, value, traceback):
        if threading.currentThread() is threading.main_thread():
            signal.signal(signal.SIGINT, self._old_handler)

            if self._signal_received:
                self._old_handler(*self._signal_received)
Example #50
0
 def is_main_thread():
     return current_thread() is main_thread()
Example #51
0
from math import atan2, degrees, radians
from time import sleep, time
from util import math_utils

import hebi
import sys
import threading

# ------------------------------------------------------------------------------
# Helper Functions
# ------------------------------------------------------------------------------

# Used for Igor background controller thread
if sys.version_info[0] == 3:
    is_main_thread_active = lambda: threading.main_thread().is_alive()
else:
    is_main_thread_active = lambda: any(
        (i.name == "MainThread") and i.is_alive()
        for i in threading.enumerate())


def retry_on_error(func, on_error_func=None, sleep_time=0.1):
    """
  Call the input function until it succeeds,
  sleeping on failure by the specified amount.
  """
    if not hasattr(func, '__call__'):
        raise TypeError()
    while True:
        try:
Example #52
0
async def spawn_tasks(
    lifecycle: Optional[Callable] = None,
    registry: Optional[registries.GlobalRegistry] = None,
    standalone: bool = False,
    priority: int = 0,
    peering_name: Optional[str] = None,
    namespace: Optional[str] = None,
) -> Collection[asyncio.Task]:
    """
    Spawn all the tasks needed to run the operator.

    The tasks are properly inter-connected with the synchronisation primitives.
    """
    loop = asyncio.get_running_loop()

    # The freezer and the registry are scoped to this whole task-set, to sync them all.
    lifecycle = lifecycle if lifecycle is not None else lifecycles.get_default_lifecycle(
    )
    registry = registry if registry is not None else registries.get_default_registry(
    )
    event_queue = asyncio.Queue(loop=loop)
    freeze_flag = asyncio.Event(loop=loop)
    should_stop = asyncio.Future(loop=loop)
    tasks = []

    # A top-level task for external stopping by setting a stop-flag. Once set,
    # this task will exit, and thus all other top-level tasks will be cancelled.
    tasks.extend([
        loop.create_task(_stop_flag_checker(should_stop)),
    ])

    # K8s-event posting. Events are queued in-memory and posted in the background.
    # NB: currently, it is a global task, but can be made per-resource or per-object.
    tasks.extend([
        loop.create_task(
            _root_task_checker("poster of events",
                               posting.poster(event_queue=event_queue))),
    ])

    # Monitor the peers, unless explicitly disabled.
    ourselves: Optional[peering.Peer] = peering.Peer.detect(
        id=peering.detect_own_id(),
        priority=priority,
        standalone=standalone,
        namespace=namespace,
        name=peering_name,
    )
    if ourselves:
        tasks.extend([
            loop.create_task(peering.peers_keepalive(ourselves=ourselves)),
            loop.create_task(
                _root_task_checker(
                    "watcher of peering",
                    queueing.watcher(
                        namespace=namespace,
                        resource=ourselves.resource,
                        handler=functools.partial(
                            peering.peers_handler,
                            ourselves=ourselves,
                            freeze=freeze_flag)))),  # freeze is set/cleared
        ])

    # Resource event handling, only once for every known resource (de-duplicated).
    for resource in registry.resources:
        tasks.extend([
            loop.create_task(
                _root_task_checker(
                    f"watcher of {resource.name}",
                    queueing.watcher(
                        namespace=namespace,
                        resource=resource,
                        handler=functools.partial(
                            handling.custom_object_handler,
                            lifecycle=lifecycle,
                            registry=registry,
                            resource=resource,
                            event_queue=event_queue,
                            freeze=freeze_flag)))),  # freeze is only checked
        ])

    # On Ctrl+C or pod termination, cancel all tasks gracefully.
    if threading.current_thread() is threading.main_thread():
        loop.add_signal_handler(signal.SIGINT, should_stop.set_result,
                                signal.SIGINT)
        loop.add_signal_handler(signal.SIGTERM, should_stop.set_result,
                                signal.SIGTERM)
    else:
        logger.warning(
            "OS signals are ignored: running not in the main thread.")

    return tasks
Example #53
0
def call_soon(func, loop):
    if threading.current_thread() is threading.main_thread():
        func()
    else:
        loop.call_soon_threadsafe(func)
 def default_threads():
     return {
         threading.current_thread().getName(),
         threading.main_thread().getName()
     }
Example #55
0
 def errorMsg(message):
     if threading.current_thread() != threading.main_thread():
         return WndUtils.call_in_main_thread(WndUtils.displayMessage, QMessageBox.Critical, message)
     else:
         return WndUtils.displayMessage(QMessageBox.Critical, message)
Example #56
0
 def run(self):
     """ runs the controller loop
     """
     while threading.main_thread().is_alive():
         self.step()
Example #57
0
def is_main_thread():
    if six.PY2:
        return isinstance(threading.current_thread(), threading._MainThread)
    else:
        # a nicer solution with py3
        return threading.current_thread() == threading.main_thread()
def import_latest_data(config, append_to_csv = False, periodic_read = False):
    """Reads the latest data from the Wasserschutzpolizei Zurich weather API

    Parameters:
    config (Config): The Config containing the DB connection info and CSV folder info
    append_to_csv (bool): Defines if the data should be appended to a CSV file
    periodic_read (bool): Defines if the function should keep reading after it imported the latest data (blocking through a sleep)

   """
    # access API for current data
    current_time = datetime.utcnow() + timedelta(hours = 1)
    current_day = current_time.replace(hour = 0, minute = 0, second = 0, microsecond = 0)
    last_db_days = [current_day] * len(config.stations)

    for idx, station in enumerate(config.stations):
        last_db_entry = __get_last_db_entry(config, station)
        last_db_days[idx] = __extract_last_db_day(last_db_entry, station, last_db_days[idx]) + timedelta(hours = 1)

    if periodic_read and threading.current_thread() is threading.main_thread():
        signal.signal(signal.SIGINT, __signal_handler)
        print('\nPress Ctrl+C to stop!\n')

    check_db_day = min(last_db_days)
    check_db_day = check_db_day.replace(hour = 0, minute = 0, second = 0, microsecond = 0)

    first_cycle = True
    last_cycle = False

    while True:
        # check if all historic data (retrieved from API) has been processed
        if not first_cycle and periodic_read and check_db_day >= current_day and not first_cycle:
            # once every 10 Min
            current_time = datetime.utcnow() + timedelta(hours = 1)
            sleep_until = current_time + timedelta(minutes = 10)
            # once per day
            # sleep_until = current_time + timedelta(days = 1)
            # sleep_until = sleep_until.replace(hour = 6, minute = 0, second = 0, microsecond = 0)
            sleep_sec = (sleep_until - current_time).total_seconds()

            print('Sleep for ' + str(sleep_sec) + 's (from ' + str(current_time) + ' until ' + str(sleep_until) + ') when next data will be queried.')
            sleep(sleep_sec)
            current_day = current_time.replace(hour = 0, minute = 0, second = 0, microsecond = 0)

        if not periodic_read and check_db_day >= current_day:
            if last_cycle:
                return
            last_cycle = True

        for idx, station in enumerate(config.stations):
            if last_db_days[idx].replace(hour = 0, minute = 0, second = 0, microsecond = 0) > check_db_day:
                continue
            last_db_entry = __get_last_db_entry(config, station)
            last_db_days[idx] = __extract_last_db_day(last_db_entry, station, last_db_days[idx])
            data_of_last_db_day = __get_data_of_day(check_db_day, station)

            normalized_data = __clean_data(config, data_of_last_db_day, last_db_entry, station)

            if normalized_data.size > 0:
                __add_data_to_db(config, normalized_data, station)
                if append_to_csv:
                    # save data as cet
                    normalized_data['timestamp_cet'] = normalized_data.index + timedelta(hours = 1)
                    normalized_data.set_index('timestamp_cet', inplace = True)

                    __append_df_to_csv(normalized_data, os.path.join(config.historic_data_folder ,'messwerte_' + station + '_' + str(normalized_data.index[0].year) + '.csv'))
                print('Handle ' + station + ' from ' + str(normalized_data.index[0]) + ' to ' + str(normalized_data.index[-1]))
            else:
                print('No new data received for ' + station)

        if check_db_day < current_day:
            check_db_day = check_db_day + pd.DateOffset(1)
        elif periodic_read and check_db_day >= current_day:
            check_db_day = datetime.utcnow() + timedelta(hours = 1)

        if first_cycle:
            first_cycle = False
Example #59
0
def run(
    run_or_experiment: Union[str, Callable, Type],
    name: Optional[str] = None,
    metric: Optional[str] = None,
    mode: Optional[str] = None,
    stop: Optional[Union[Mapping, Stopper, Callable[[str, Mapping], bool]]] = None,
    time_budget_s: Optional[Union[int, float, datetime.timedelta]] = None,
    config: Optional[Dict[str, Any]] = None,
    resources_per_trial: Union[
        None, Mapping[str, Union[float, int, Mapping]], PlacementGroupFactory
    ] = None,
    num_samples: int = 1,
    local_dir: Optional[str] = None,
    search_alg: Optional[Union[Searcher, SearchAlgorithm, str]] = None,
    scheduler: Optional[Union[TrialScheduler, str]] = None,
    keep_checkpoints_num: Optional[int] = None,
    checkpoint_score_attr: Optional[str] = None,
    checkpoint_freq: int = 0,
    checkpoint_at_end: bool = False,
    verbose: Union[int, Verbosity] = Verbosity.V3_TRIAL_DETAILS,
    progress_reporter: Optional[ProgressReporter] = None,
    log_to_file: bool = False,
    trial_name_creator: Optional[Callable[[Trial], str]] = None,
    trial_dirname_creator: Optional[Callable[[Trial], str]] = None,
    sync_config: Optional[SyncConfig] = None,
    export_formats: Optional[Sequence] = None,
    max_failures: int = 0,
    fail_fast: bool = False,
    restore: Optional[str] = None,
    server_port: Optional[int] = None,
    resume: Union[bool, str] = False,
    reuse_actors: Optional[bool] = None,
    trial_executor: Optional[RayTrialExecutor] = None,
    raise_on_failed_trial: bool = True,
    callbacks: Optional[Sequence[Callback]] = None,
    max_concurrent_trials: Optional[int] = None,
    # == internal only ==
    _experiment_checkpoint_dir: Optional[str] = None,
    # Deprecated args
    queue_trials: Optional[bool] = None,
    loggers: Optional[Sequence[Type[Logger]]] = None,
    _remote: Optional[bool] = None,
) -> ExperimentAnalysis:
    """Executes training.

    When a SIGINT signal is received (e.g. through Ctrl+C), the tuning run
    will gracefully shut down and checkpoint the latest experiment state.
    Sending SIGINT again (or SIGKILL/SIGTERM instead) will skip this step.

    Many aspects of Tune, such as the frequency of global checkpointing,
    maximum pending placement group trials and the path of the result
    directory be configured through environment variables. Refer to
    :ref:`tune-env-vars` for a list of environment variables available.

    Examples:

    .. code-block:: python

        # Run 10 trials (each trial is one instance of a Trainable). Tune runs
        # in parallel and automatically determines concurrency.
        tune.run(trainable, num_samples=10)

        # Run 1 trial, stop when trial has reached 10 iterations
        tune.run(my_trainable, stop={"training_iteration": 10})

        # automatically retry failed trials up to 3 times
        tune.run(my_trainable, stop={"training_iteration": 10}, max_failures=3)

        # Run 1 trial, search over hyperparameters, stop after 10 iterations.
        space = {"lr": tune.uniform(0, 1), "momentum": tune.uniform(0, 1)}
        tune.run(my_trainable, config=space, stop={"training_iteration": 10})

        # Resumes training if a previous machine crashed
        tune.run(my_trainable, config=space,
                 local_dir=<path/to/dir>, resume=True)

        # Rerun ONLY failed trials after an experiment is finished.
        tune.run(my_trainable, config=space,
                 local_dir=<path/to/dir>, resume="ERRORED_ONLY")

    Args:
        run_or_experiment: If function|class|str, this is the algorithm or
            model to train. This may refer to the name of a built-on algorithm
            (e.g. RLlib's DQN or PPO), a user-defined trainable
            function or class, or the string identifier of a
            trainable function or class registered in the tune registry.
            If Experiment, then Tune will execute training based on
            Experiment.spec. If you want to pass in a Python lambda, you
            will need to first register the function:
            ``tune.register_trainable("lambda_id", lambda x: ...)``. You can
            then use ``tune.run("lambda_id")``.
        metric: Metric to optimize. This metric should be reported
            with `tune.report()`. If set, will be passed to the search
            algorithm and scheduler.
        mode: Must be one of [min, max]. Determines whether objective is
            minimizing or maximizing the metric attribute. If set, will be
            passed to the search algorithm and scheduler.
        name: Name of experiment.
        stop: Stopping criteria. If dict,
            the keys may be any field in the return result of 'train()',
            whichever is reached first. If function, it must take (trial_id,
            result) as arguments and return a boolean (True if trial should be
            stopped, False otherwise). This can also be a subclass of
            ``ray.tune.Stopper``, which allows users to implement
            custom experiment-wide stopping (i.e., stopping an entire Tune
            run based on some time constraint).
        time_budget_s: Global time budget in
            seconds after which all trials are stopped. Can also be a
            ``datetime.timedelta`` object.
        config: Algorithm-specific configuration for Tune variant
            generation (e.g. env, hyperparams). Defaults to empty dict.
            Custom search algorithms may ignore this.
        resources_per_trial: Machine resources
            to allocate per trial, e.g. ``{"cpu": 64, "gpu": 8}``.
            Note that GPUs will not be assigned unless you specify them here.
            Defaults to 1 CPU and 0 GPUs in
            ``Trainable.default_resource_request()``. This can also
            be a PlacementGroupFactory object wrapping arguments to create a
            per-trial placement group.
        num_samples: Number of times to sample from the
            hyperparameter space. Defaults to 1. If `grid_search` is
            provided as an argument, the grid will be repeated
            `num_samples` of times. If this is -1, (virtually) infinite
            samples are generated until a stopping condition is met.
        local_dir: Local dir to save training results to.
            Defaults to ``~/ray_results``.
        search_alg: Search algorithm for
            optimization. You can also use the name of the algorithm.
        scheduler: Scheduler for executing
            the experiment. Choose among FIFO (default), MedianStopping,
            AsyncHyperBand, HyperBand and PopulationBasedTraining. Refer to
            ray.tune.schedulers for more options. You can also use the
            name of the scheduler.
        keep_checkpoints_num: Number of checkpoints to keep. A value of
            `None` keeps all checkpoints. Defaults to `None`. If set, need
            to provide `checkpoint_score_attr`.
        checkpoint_score_attr: Specifies by which attribute to rank the
            best checkpoint. Default is increasing order. If attribute starts
            with `min-` it will rank attribute in decreasing order, i.e.
            `min-validation_loss`.
        checkpoint_freq: How many training iterations between
            checkpoints. A value of 0 (default) disables checkpointing.
            This has no effect when using the Functional Training API.
        checkpoint_at_end: Whether to checkpoint at the end of the
            experiment regardless of the checkpoint_freq. Default is False.
            This has no effect when using the Functional Training API.
        verbose: 0, 1, 2, or 3. Verbosity mode.
            0 = silent, 1 = only status updates, 2 = status and brief trial
            results, 3 = status and detailed trial results. Defaults to 3.
        progress_reporter: Progress reporter for reporting
            intermediate experiment progress. Defaults to CLIReporter if
            running in command-line, or JupyterNotebookReporter if running in
            a Jupyter notebook.
        log_to_file: Log stdout and stderr to files in
            Tune's trial directories. If this is `False` (default), no files
            are written. If `true`, outputs are written to `trialdir/stdout`
            and `trialdir/stderr`, respectively. If this is a single string,
            this is interpreted as a file relative to the trialdir, to which
            both streams are written. If this is a Sequence (e.g. a Tuple),
            it has to have length 2 and the elements indicate the files to
            which stdout and stderr are written, respectively.
        trial_name_creator: Optional function
            for generating the trial string representation.
        trial_dirname_creator: Function
            for generating the trial dirname. This function should take
            in a Trial object and return a string representing the
            name of the directory. The return value cannot be a path.
        sync_config: Configuration object for syncing. See
            tune.SyncConfig.
        export_formats: List of formats that exported at the end of
            the experiment. Default is None.
        max_failures: Try to recover a trial at least this many times.
            Ray will recover from the latest checkpoint if present.
            Setting to -1 will lead to infinite recovery retries.
            Setting to 0 will disable retries. Defaults to 0.
        fail_fast: Whether to fail upon the first error.
            If fail_fast='raise' provided, Tune will automatically
            raise the exception received by the Trainable. fail_fast='raise'
            can easily leak resources and should be used with caution (it
            is best used with `ray.init(local_mode=True)`).
        restore: Path to checkpoint. Only makes sense to set if
            running 1 trial. Defaults to None.
        server_port: Port number for launching TuneServer.
        resume: One of "LOCAL", "REMOTE", "PROMPT", "ERRORED_ONLY", "AUTO",
            or bool. "LOCAL"/True restores the checkpoint from the
            local experiment directory, determined
            by ``name`` and ``local_dir``. "REMOTE" restores the checkpoint
            from ``upload_dir`` (as passed to ``sync_config``).
            "PROMPT" provides the CLI feedback.
            False forces a new experiment. "ERRORED_ONLY" resets and reruns
            errored trials upon resume - previous trial artifacts will
            be left untouched.
            "AUTO" will attempt to resume from a checkpoint and otherwise
            start a new experiment.
            If resume is set but checkpoint does not exist,
            ValueError will be thrown.
        reuse_actors: Whether to reuse actors between different trials
            when possible. This can drastically speed up experiments that start
            and stop actors often (e.g., PBT in time-multiplexing mode). This
            requires trials to have the same resource requirements.
            Defaults to ``True`` for function trainables and ``False`` for
            class and registered trainables.
        trial_executor: Manage the execution of trials.
        raise_on_failed_trial: Raise TuneError if there exists failed
            trial (of ERROR state) when the experiments complete.
        callbacks: List of callbacks that will be called at different
            times in the training loop. Must be instances of the
            ``ray.tune.callback.Callback`` class. If not passed,
            `LoggerCallback` and `SyncerCallback` callbacks are automatically
            added.
        max_concurrent_trials: Maximum number of trials to run
            concurrently. Must be non-negative. If None or 0, no limit will
            be applied. This is achieved by wrapping the ``search_alg`` in
            a :class:`ConcurrencyLimiter`, and thus setting this argument
            will raise an exception if the ``search_alg`` is already a
            :class:`ConcurrencyLimiter`. Defaults to None.
        _remote: Whether to run the Tune driver in a remote function.
            This is disabled automatically if a custom trial executor is
            passed in. This is enabled by default in Ray client mode.

    Returns:
        ExperimentAnalysis: Object for experiment analysis.

    Raises:
        TuneError: Any trials failed and `raise_on_failed_trial` is True.
    """

    # To be removed in 1.9.
    if queue_trials is not None:
        raise DeprecationWarning(
            "`queue_trials` has been deprecated and is replaced by "
            "the `TUNE_MAX_PENDING_TRIALS_PG` environment variable. "
            "Per default at least one Trial is queued at all times, "
            "so you likely don't need to change anything other than "
            "removing this argument from your call to `tune.run()`"
        )

    # Starting deprecation in ray 1.10.
    if os.environ.get("TUNE_TRIAL_RESULT_WAIT_TIME_S") is not None:
        warnings.warn("`TUNE_TRIAL_RESULT_WAIT_TIME_S` is deprecated.")

    if os.environ.get("TUNE_TRIAL_STARTUP_GRACE_PERIOD") is not None:
        warnings.warn("`TUNE_TRIAL_STARTUP_GRACE_PERIOD` is deprecated.")

    if os.environ.get("TUNE_PLACEMENT_GROUP_WAIT_S") is not None:
        warnings.warn("`TUNE_PLACEMENT_GROUP_WAIT_S` is deprecated.")

    # NO CODE IS TO BE ADDED ABOVE THIS COMMENT
    # remote_run_kwargs must be defined before any other
    # code is ran to ensure that at this point,
    # `locals()` is equal to args and kwargs
    remote_run_kwargs = locals().copy()
    remote_run_kwargs.pop("_remote")

    if _remote is None:
        _remote = ray.util.client.ray.is_connected()

    if _remote is True and trial_executor:
        raise ValueError("cannot use custom trial executor")

    if not trial_executor or isinstance(trial_executor, RayTrialExecutor):
        _ray_auto_init()

    if _remote:
        remote_run = ray.remote(num_cpus=0)(run)

        # Make sure tune.run is called on the sever node.
        remote_run = force_on_current_node(remote_run)

        # JupyterNotebooks don't work with remote tune runs out of the box
        # (e.g. via Ray client) as they don't have access to the main
        # process stdout. So we introduce a queue here that accepts
        # callables, which will then be executed on the driver side.
        if isinstance(progress_reporter, JupyterNotebookReporter):
            execute_queue = Queue(
                actor_options={"num_cpus": 0, **force_on_current_node(None)}
            )
            progress_reporter.set_output_queue(execute_queue)

            def get_next_queue_item():
                try:
                    return execute_queue.get(block=False)
                except Empty:
                    return None

        else:
            # If we don't need a queue, use this dummy get fn instead of
            # scheduling an unneeded actor
            def get_next_queue_item():
                return None

        def _handle_execute_queue():
            execute_item = get_next_queue_item()
            while execute_item:
                if isinstance(execute_item, Callable):
                    execute_item()

                execute_item = get_next_queue_item()

        remote_future = remote_run.remote(_remote=False, **remote_run_kwargs)

        # ray.wait(...)[1] returns futures that are not ready, yet
        while ray.wait([remote_future], timeout=0.2)[1]:
            # Check if we have items to execute
            _handle_execute_queue()

        # Handle queue one last time
        _handle_execute_queue()

        return ray.get(remote_future)

    del remote_run_kwargs

    all_start = time.time()

    if loggers:
        # Raise DeprecationWarning in 1.9, remove in 1.10/1.11
        warnings.warn(
            "The `loggers` argument is deprecated. Please pass the respective "
            "`LoggerCallback` classes to the `callbacks` argument instead. "
            "See https://docs.ray.io/en/latest/tune/api_docs/logging.html"
        )

    if mode and mode not in ["min", "max"]:
        raise ValueError(
            "The `mode` parameter passed to `tune.run()` has to be one of "
            "['min', 'max']"
        )

    set_verbosity(verbose)

    config = config or {}
    sync_config = sync_config or SyncConfig()
    validate_upload_dir(sync_config)
    set_sync_periods(sync_config)

    if num_samples == -1:
        num_samples = sys.maxsize

    result_buffer_length = None

    # Create scheduler here as we need access to some of its properties
    if isinstance(scheduler, str):
        # importing at top level causes a recursive dependency
        from ray.tune.schedulers import create_scheduler

        scheduler = create_scheduler(scheduler)
    scheduler = scheduler or FIFOScheduler()

    if not scheduler.supports_buffered_results:
        # Result buffering with e.g. a Hyperband scheduler is a bad idea, as
        # hyperband tries to stop trials when processing brackets. With result
        # buffering, we might trigger this multiple times when evaluating
        # a single trial, which leads to unexpected behavior.
        env_result_buffer_length = os.getenv("TUNE_RESULT_BUFFER_LENGTH", "")
        if env_result_buffer_length:
            warnings.warn(
                f"You are using a {type(scheduler)} scheduler, but "
                f"TUNE_RESULT_BUFFER_LENGTH is set "
                f"({env_result_buffer_length}). This can lead to undesired "
                f"and faulty behavior, so the buffer length was forcibly set "
                f"to 1 instead."
            )
        result_buffer_length = 1

    # If reuse_actors is unset, default to False for string and class trainables,
    # and default to True for everything else (i.e. function trainables)
    if reuse_actors is None:
        trainable = (
            run_or_experiment.run_identifier
            if isinstance(run_or_experiment, Experiment)
            else run_or_experiment
        )
        reuse_actors = (
            # Only default to True for function trainables that meet certain conditions
            is_function_trainable(trainable)
            and not (
                # Changing resources requires restarting actors
                scheduler
                and isinstance(scheduler, ResourceChangingScheduler)
            )
            and not (
                # If GPUs are requested we could run into problems with device memory
                _check_gpus_in_resources(resources_per_trial)
            )
            and not (
                # If the resource request is overridden, we don't know if GPUs
                # will be requested, yet, so default to False
                _check_default_resources_override(trainable)
            )
        )

    if (
        isinstance(scheduler, (PopulationBasedTraining, PopulationBasedTrainingReplay))
        and not reuse_actors
    ):
        warnings.warn(
            "Consider boosting PBT performance by enabling `reuse_actors` as "
            "well as implementing `reset_config` for Trainable."
        )

    trial_executor = trial_executor or RayTrialExecutor(
        reuse_actors=reuse_actors, result_buffer_length=result_buffer_length
    )
    if isinstance(run_or_experiment, list):
        experiments = run_or_experiment
    else:
        experiments = [run_or_experiment]

    for i, exp in enumerate(experiments):
        if not isinstance(exp, Experiment):
            experiments[i] = Experiment(
                name=name,
                run=exp,
                stop=stop,
                time_budget_s=time_budget_s,
                config=config,
                resources_per_trial=resources_per_trial,
                num_samples=num_samples,
                local_dir=local_dir,
                _experiment_checkpoint_dir=_experiment_checkpoint_dir,
                sync_config=sync_config,
                trial_name_creator=trial_name_creator,
                trial_dirname_creator=trial_dirname_creator,
                log_to_file=log_to_file,
                checkpoint_freq=checkpoint_freq,
                checkpoint_at_end=checkpoint_at_end,
                keep_checkpoints_num=keep_checkpoints_num,
                checkpoint_score_attr=checkpoint_score_attr,
                export_formats=export_formats,
                max_failures=max_failures,
                restore=restore,
            )
    else:
        logger.debug("Ignoring some parameters passed into tune.run.")

    if fail_fast and max_failures != 0:
        raise ValueError("max_failures must be 0 if fail_fast=True.")

    if isinstance(search_alg, str):
        # importing at top level causes a recursive dependency
        from ray.tune.suggest import create_searcher

        search_alg = create_searcher(search_alg)

    # if local_mode=True is set during ray.init().
    is_local_mode = ray.worker._mode() == ray.worker.LOCAL_MODE

    if is_local_mode:
        max_concurrent_trials = 1

    if not search_alg:
        search_alg = BasicVariantGenerator(max_concurrent=max_concurrent_trials or 0)
    elif max_concurrent_trials or is_local_mode:
        if isinstance(search_alg, ConcurrencyLimiter):
            if not is_local_mode:
                if search_alg.max_concurrent != max_concurrent_trials:
                    raise ValueError(
                        "You have specified `max_concurrent_trials="
                        f"{max_concurrent_trials}`, but the `search_alg` is "
                        "already a `ConcurrencyLimiter` with `max_concurrent="
                        f"{search_alg.max_concurrent}. FIX THIS by setting "
                        "`max_concurrent_trials=None`."
                    )
                else:
                    logger.warning(
                        "You have specified `max_concurrent_trials="
                        f"{max_concurrent_trials}`, but the `search_alg` is "
                        "already a `ConcurrencyLimiter`. "
                        "`max_concurrent_trials` will be ignored."
                    )
        else:
            if max_concurrent_trials < 1:
                raise ValueError(
                    "`max_concurrent_trials` must be greater or equal than 1, "
                    f"got {max_concurrent_trials}."
                )
            if isinstance(search_alg, Searcher):
                search_alg = ConcurrencyLimiter(
                    search_alg, max_concurrent=max_concurrent_trials
                )
            elif not is_local_mode:
                logger.warning(
                    "You have passed a `SearchGenerator` instance as the "
                    "`search_alg`, but `max_concurrent_trials` requires a "
                    "`Searcher` instance`. `max_concurrent_trials` "
                    "will be ignored."
                )

    if isinstance(search_alg, Searcher):
        search_alg = SearchGenerator(search_alg)

    if config and not searcher_set_search_properties_backwards_compatible(
        search_alg.set_search_properties,
        metric,
        mode,
        config,
        **experiments[0].public_spec,
    ):
        if has_unresolved_values(config):
            raise ValueError(
                "You passed a `config` parameter to `tune.run()` with "
                "unresolved parameters, but the search algorithm was already "
                "instantiated with a search space. Make sure that `config` "
                "does not contain any more parameter definitions - include "
                "them in the search algorithm's search space if necessary."
            )

    if not scheduler_set_search_properties_backwards_compatible(
        scheduler.set_search_properties, metric, mode, **experiments[0].public_spec
    ):
        raise ValueError(
            "You passed a `metric` or `mode` argument to `tune.run()`, but "
            "the scheduler you are using was already instantiated with their "
            "own `metric` and `mode` parameters. Either remove the arguments "
            "from your scheduler or from your call to `tune.run()`"
        )

    # Create syncer callbacks
    callbacks = create_default_callbacks(
        callbacks, sync_config, metric=metric, loggers=loggers
    )

    runner = TrialRunner(
        search_alg=search_alg,
        scheduler=scheduler,
        local_checkpoint_dir=experiments[0].checkpoint_dir,
        remote_checkpoint_dir=experiments[0].remote_checkpoint_dir,
        sync_config=sync_config,
        stopper=experiments[0].stopper,
        resume=resume,
        server_port=server_port,
        fail_fast=fail_fast,
        trial_executor=trial_executor,
        callbacks=callbacks,
        metric=metric,
        # Driver should only sync trial checkpoints if
        # checkpoints are not synced to cloud
        driver_sync_trial_checkpoints=not bool(sync_config.upload_dir),
    )

    if not runner.resumed:
        for exp in experiments:
            search_alg.add_configurations([exp])
    else:
        logger.info(
            "TrialRunner resumed, ignoring new add_experiment but "
            "updating trial resources."
        )
        if resources_per_trial:
            runner.update_pending_trial_resources(resources_per_trial)

    # Calls setup on callbacks
    runner.setup_experiments(
        experiments=experiments, total_num_samples=search_alg.total_samples
    )

    # User Warning for GPUs
    if trial_executor.has_gpus():
        if _check_gpus_in_resources(resources=resources_per_trial):
            # "gpu" is manually set.
            pass
        elif _check_default_resources_override(experiments[0].run_identifier):
            # "default_resources" is manually overridden.
            pass
        else:
            logger.warning(
                "Tune detects GPUs, but no trials are using GPUs. "
                "To enable trials to use GPUs, set "
                "tune.run(resources_per_trial={'gpu': 1}...) "
                "which allows Tune to expose 1 GPU to each trial. "
                "You can also override "
                "`Trainable.default_resource_request` if using the "
                "Trainable API."
            )

    original_handler = signal.getsignal(signal.SIGINT)
    state = {"signal": None}

    def signal_interrupt_tune_run(sig: int, frame):
        logger.warning(
            "Stop signal received (e.g. via SIGINT/Ctrl+C), ending Ray Tune run. "
            "This will try to checkpoint the experiment state one last time. "
            "Press CTRL+C (or send SIGINT/SIGKILL/SIGTERM) "
            "to skip. "
        )
        state["signal"] = sig
        # Restore original signal handler to react to future SIGINT signals
        signal.signal(signal.SIGINT, original_handler)

    # We should only install the handler when it is safe to do so.
    # When tune.run() is called from worker thread, signal.signal will
    # fail.
    allow_signal_catching = True
    if threading.current_thread() != threading.main_thread():
        allow_signal_catching = False

    if allow_signal_catching:
        if not int(os.getenv("TUNE_DISABLE_SIGINT_HANDLER", "0")):
            signal.signal(signal.SIGINT, signal_interrupt_tune_run)

        # Always register SIGUSR1 if available (not available e.g. on Windows)
        if hasattr(signal, "SIGUSR1"):
            signal.signal(signal.SIGUSR1, signal_interrupt_tune_run)

    progress_reporter = progress_reporter or detect_reporter()

    tune_start = time.time()

    progress_reporter.setup(
        start_time=tune_start,
        total_samples=search_alg.total_samples,
        metric=metric,
        mode=mode,
    )
    while not runner.is_finished() and not state["signal"]:
        runner.step()
        if has_verbosity(Verbosity.V1_EXPERIMENT):
            _report_progress(runner, progress_reporter)
    tune_taken = time.time() - tune_start

    try:
        runner.checkpoint(force=True)
    except Exception as e:
        logger.warning(f"Trial Runner checkpointing failed: {str(e)}")

    if has_verbosity(Verbosity.V1_EXPERIMENT):
        _report_progress(runner, progress_reporter, done=True)

    wait_for_sync()
    runner.cleanup()

    incomplete_trials = []
    for trial in runner.get_trials():
        if trial.status != Trial.TERMINATED:
            incomplete_trials += [trial]

    if incomplete_trials:
        if raise_on_failed_trial and not state["signal"]:
            raise TuneError("Trials did not complete", incomplete_trials)
        else:
            logger.error("Trials did not complete: %s", incomplete_trials)

    all_taken = time.time() - all_start
    if has_verbosity(Verbosity.V1_EXPERIMENT):
        logger.info(
            f"Total run time: {all_taken:.2f} seconds "
            f"({tune_taken:.2f} seconds for the tuning loop)."
        )

    if state["signal"]:
        logger.warning(
            "Experiment has been interrupted, but the most recent state was "
            "saved. You can continue running this experiment by passing "
            "`resume=True` to `tune.run()`"
        )

    trials = runner.get_trials()
    return ExperimentAnalysis(
        runner.checkpoint_file,
        trials=trials,
        default_metric=metric,
        default_mode=mode,
        sync_config=sync_config,
    )
Example #60
0
    def __init__(self,
                 toplevel,
                 module,
                 work_dir=None,
                 python_search=None,
                 toplevel_lang="verilog",
                 verilog_sources=None,
                 vhdl_sources=None,
                 includes=None,
                 defines=None,
                 parameters=None,
                 compile_args=None,
                 sim_args=None,
                 extra_args=None,
                 plus_args=None,
                 force_compile=False,
                 testcase=None,
                 sim_build="sim_build",
                 seed=None,
                 extra_env=None,
                 compile_only=False,
                 waves=None,
                 gui=False,
                 simulation_args=None,
                 **kwargs):

        self.sim_dir = os.path.abspath(sim_build)
        os.makedirs(self.sim_dir, exist_ok=True)

        self.logger = logging.getLogger("cocotb")
        self.logger.setLevel(logging.INFO)
        logging.basicConfig(format="%(levelname)s %(name)s: %(message)s")

        warnings.simplefilter('always', DeprecationWarning)

        self.lib_dir = os.path.join(os.path.dirname(cocotb.__file__), "libs")

        self.lib_ext = "so"
        if os.name == "nt":
            self.lib_ext = "dll"

        self.module = module  # TODO: Auto discovery, try introspect ?

        self.work_dir = self.sim_dir

        if work_dir is not None:
            absworkdir = os.path.abspath(work_dir)
            if os.path.isdir(absworkdir):
                self.work_dir = absworkdir

        if python_search is None:
            python_search = []

        self.python_search = python_search

        self.toplevel = toplevel
        self.toplevel_lang = toplevel_lang

        if verilog_sources is None:
            verilog_sources = []

        self.verilog_sources = self.get_abs_paths(verilog_sources)

        if vhdl_sources is None:
            vhdl_sources = []

        self.vhdl_sources = self.get_abs_paths(vhdl_sources)

        if includes is None:
            includes = []

        self.includes = self.get_abs_paths(includes)

        if defines is None:
            defines = []

        self.defines = defines

        if parameters is None:
            parameters = {}

        self.parameters = parameters

        if compile_args is None:
            compile_args = []

        if extra_args is None:
            extra_args = []

        self.compile_args = compile_args + extra_args

        if sim_args is None:
            sim_args = []

        if simulation_args is not None:
            sim_args += simulation_args
            warnings.warn(
                "Using simulation_args is deprecated. Please use sim_args instead.",
                DeprecationWarning,
                stacklevel=2)

        self.simulation_args = sim_args + extra_args

        if plus_args is None:
            plus_args = []

        self.plus_args = plus_args
        self.force_compile = force_compile
        self.compile_only = compile_only

        if kwargs:
            warnings.warn(
                "Using kwargs is deprecated. Please explicitly declare or arguments instead.",
                DeprecationWarning,
                stacklevel=2)

        for arg in kwargs:
            setattr(self, arg, kwargs[arg])

        # by copy since we modify
        self.env = dict(extra_env) if extra_env is not None else {}

        if testcase is not None:
            self.env["TESTCASE"] = testcase

        if seed is not None:
            self.env["RANDOM_SEED"] = str(seed)

        if waves is None:
            self.waves = bool(int(os.getenv("WAVES", 0)))
        else:
            self.waves = bool(waves)

        self.gui = gui

        # Catch SIGINT and SIGTERM
        self.old_sigint_h = signal.getsignal(signal.SIGINT)
        self.old_sigterm_h = signal.getsignal(signal.SIGTERM)

        # works only if main thread
        if threading.current_thread() is threading.main_thread():
            signal.signal(signal.SIGINT, self.exit_gracefully)
            signal.signal(signal.SIGTERM, self.exit_gracefully)