コード例 #1
0
    def test_decoder_failure(self):
        """
        This test checks that the decoder works properly whenever random data in given to it.

        :return:
        """

        async def run(q):
            try:
                s = asyncio.Semaphore(0)

                def error_callback():
                    q.put(None)
                    s.release()

                decoder = Decoder(error_callback)

                data = os.urandom(1000)

                await decoder.digest(struct.pack(">I", len(data)))
                await decoder.digest(data)

                s.acquire()
                q.put(None)
            except:
                q.put(sys.exc_info())

        q = Queue()
        loop = get_event_loop()
        asyncio.run_coroutine_threadsafe(run(q), loop)

        # If you get an Empty exception over here, it means the co-routine timed out.
        exc_info = q.get(timeout=1)
        if exc_info is not None:
            raise exc_info[1].with_traceback(exc_info[2])
コード例 #2
0
    def test_ping(self):
        async def run(q):
            try:
                connections = {}
                listener = Listener("127.0.0.1", 8888, connections)
                await listener.wait_until_started()

                client_connection = await Connection.from_host("127.0.0.1", 8888)

                # This one is optional. Any commands transmitting will wait for the connection to be ready anyways.
                await client_connection.wait_for_ready()

                await client_connection.ping()
                await client_connection.ping()

                q.put(None)
            except:
                q.put(sys.exc_info())

        q = Queue()
        loop = get_event_loop()
        asyncio.run_coroutine_threadsafe(run(q), loop)

        # If you get an Empty exception over here, it means the co-routine timed out.
        exc_info = q.get(timeout=1)
        if exc_info is not None:
            raise exc_info[1].with_traceback(exc_info[2])
コード例 #3
0
    def test_decoder(self):
        """
        This test, tests whether the decoder is working properly by given it a raw package and checking whether the
        result matches.

        """

        async def run(q):
            try:
                decoder = Decoder()
                inp_data = [0, [1, 2, 3]]

                data = umsgpack.packb(inp_data)

                await decoder.digest(struct.pack(">I", len(data)))
                await decoder.digest(data)

                out_data = await decoder.get()

                assert inp_data == out_data
                q.put(None)
            except:
                q.put(sys.exc_info())

        q = Queue()
        loop = get_event_loop()
        asyncio.run_coroutine_threadsafe(run(q), loop)

        # If you get an Empty exception over here, it means the co-routine timed out.
        exc_info = q.get(timeout=1)
        if exc_info is not None:
            raise exc_info[1].with_traceback(exc_info[2])
コード例 #4
0
ファイル: interface.py プロジェクト: vialectrum/vialectrum
    def __init__(self, network: 'Network', server: str, proxy: Optional[dict]):
        self.ready = asyncio.Future()
        self.got_disconnected = asyncio.Future()
        self.server = server
        self.host, self.port, self.protocol = deserialize_server(self.server)
        self.port = int(self.port)
        Logger.__init__(self)
        assert network.config.path
        self.cert_path = os.path.join(network.config.path, 'certs', self.host)
        self.blockchain = None
        self._requested_chunks = set()
        self.network = network
        self._set_proxy(proxy)
        self.session = None  # type: NotificationSession
        self._ipaddr_bucket = None

        self.tip_header = None
        self.tip = 0

        # Dump network messages (only for this interface).  Set at runtime from the console.
        self.debug = False

        asyncio.run_coroutine_threadsafe(
            self.network.main_taskgroup.spawn(self.run()), self.network.asyncio_loop)
        self.group = SilentTaskGroup()
コード例 #5
0
ファイル: registry.py プロジェクト: davebshow/ipython-gremlin
    def close(cls, connection_str=None):
        """Close DB connections"""
        if connection_str:
            conn = cls.connections.pop(connection_str)
            if not conn:
                raise RuntimeError('Specified conn does not exist')
            if connection_str == conn.uri:
                cls.connections.pop(conn.alias)
            else:
                cls.connections.pop(conn.uri)
            if conn is cls.current:
                cls.current = None
            conns = [conn]
        else:
            keys= set(cls.connections.keys())
            conns = set()
            for k in keys:
                conn = cls.connections.pop(k)
                conns.add(conn)
            cls.current = None

        async def go(conns):
            for conn in conns:
                await conn.close()

        asyncio.run_coroutine_threadsafe(go(conns), LOOP).result()
        LOOP.stop()
        LOOP.close()
        executor.shutdown()
コード例 #6
0
ファイル: main.py プロジェクト: UltrosBot/Ultros3K
    def _sigterm(self, *_):
        """
        Handle SIGTERM and shut down gracefully.
        """

        self.log.debug("SIGTERM caught.")
        asyncio.run_coroutine_threadsafe(self.shutdown(), self.event_loop)
コード例 #7
0
ファイル: controller.py プロジェクト: mhils/mitmproxy
 def tell(self, mtype, m):
     """
     Decorate a message with a dummy reply attribute, send it to the master,
     then return immediately.
     """
     m.reply = DummyReply()
     asyncio.run_coroutine_threadsafe(self._q.put((mtype, m)), self.loop)
コード例 #8
0
    def run(self):
        while not self._stop_ev.wait(self.interval):
            if self._last_ack + self.heartbeat_timeout < time.perf_counter():
                log.warning("Shard ID %s has stopped responding to the gateway. Closing and restarting.", self.shard_id)
                coro = self.ws.close(4000)
                f = asyncio.run_coroutine_threadsafe(coro, loop=self.ws.loop)

                try:
                    f.result()
                except Exception:
                    pass
                finally:
                    self.stop()
                    return

            data = self.get_payload()
            log.debug(self.msg, data['d'])
            coro = self.ws.send_as_json(data)
            f = asyncio.run_coroutine_threadsafe(coro, loop=self.ws.loop)
            try:
                # block until sending is complete
                total = 0
                while True:
                    try:
                        f.result(5)
                        break
                    except concurrent.futures.TimeoutError:
                        total += 5
                        log.warning(self.block_msg, total)

            except Exception:
                self.stop()
            else:
                self._last_send = time.perf_counter()
コード例 #9
0
ファイル: test_asyncio.py プロジェクト: opticoder/symmrpc
 def my_send(self, msg):
     @asyncio.coroutine
     def send():
         self.writer.write(self.serdes.serialize(msg))
         yield from self.writer.drain()
     asyncio.run_coroutine_threadsafe(send(), self.loop)
     print('client Sent')
コード例 #10
0
ファイル: network.py プロジェクト: chrisrico/electrum
    async def _start(self):
        assert not self.main_taskgroup
        self.main_taskgroup = SilentTaskGroup()
        assert not self.interface and not self.interfaces
        assert not self.connecting and not self.server_queue
        self.print_error('starting network')
        self.disconnected_servers = set([])
        self.protocol = deserialize_server(self.default_server)[2]
        self.server_queue = queue.Queue()
        self._set_proxy(deserialize_proxy(self.config.get('proxy')))
        self._set_oneserver(self.config.get('oneserver', False))
        self._start_interface(self.default_server)

        async def main():
            try:
                await self._init_headers_file()
                async with self.main_taskgroup as group:
                    await group.spawn(self._maintain_sessions())
                    [await group.spawn(job) for job in self._jobs]
            except Exception as e:
                traceback.print_exc(file=sys.stderr)
                raise e
        asyncio.run_coroutine_threadsafe(main(), self.asyncio_loop)

        self.trigger_callback('network_updated')
コード例 #11
0
ファイル: ClientCore.py プロジェクト: thethythy/Mnemopwd
    def command(self, key, value):
        """Create and enqueue a task from a command coming from UI layer"""
        task = None

        if key == "connection.open.credentials":
            try:
                # Direct execution because queue is empty at this moment
                self._open()
                task = self._task_set_credentials(*value)
            except:
                pass
        if key == "connection.close":
            task = self._task_close()
        if key == "connection.close.deletion":
            task = self._task_deletion()
        if key == "application.editblock":
            task = self._task_add_data_or_update_data(*value)
        if key == "application.deleteblock":
            task = self._task_delete_data(value)
        if key == "application.searchblock":
            task = self._task_search_data(value)
        if key == "application.exportblock":
            task = self._task_export_data()
        if key == "application.searchblock.blockvalues":
            task = self._task_get_block_values(value)

        if task is not None:
            asyncio.run_coroutine_threadsafe(self.queue.put(task), self.loop)
コード例 #12
0
ファイル: relay.py プロジェクト: Sorunome/OmnomIRC
	def send(self, n1, n2, t, m, c, s, uid):
		c = self.idToChan(c)
		if not c:
			return
		m = oirc.stripIrcColors(m)
		msg = ''
		if t == 'message':
			msg = '<{}> {}'.format(n1, m)
		elif t == 'action':
			msg = '* {} {}'.format(n1, m)
		elif t == 'join':
			msg = '* {} has joined'.format(n1)
		elif t == 'part':
			msg = '* {} has left'.format(n1)
		elif t == 'quit':
			msg = '* {} has quit ({})'.format(n1, m)
		elif t == 'mode':
			msg = '* {} set mode {}'.format(n1, m)
		elif t == 'kick':
			msg = '* {} has kicked {} ({})'.format(n1, n2, m)
		elif t == 'topic':
			msg = '* {} has changed the topic to {}'.format(n1, m)
		elif t == 'nick':
			msg = '* {} has changed nicks to {}'.format(n1, n2)
		if not msg:
			return
		prefix = self.getNetworkPrefix(s)
		asyncio.run_coroutine_threadsafe(self.client.send_message(c, prefix['prefix'] + msg), self.loop)
コード例 #13
0
ファイル: app.py プロジェクト: tryexceptpass/sofi
    async def process(self, client, event):
        """Process a new event"""

        eventtype = event['event']
        event['client'] = client

        if eventtype == 'response':
            # We receive a response to a specific property, attr or text request
            if 'request_id' in event:
                self.requests[event['request_id']] = event
                return

        if eventtype in self.handlers:
            # Check for local handler
            if 'key' in event:
                key = event['key']

                if key in self.handlers[eventtype]:
                    for handler in list(self.handlers[eventtype][key]):
                        if callable(handler):
                            asyncio.run_coroutine_threadsafe(handler(event), self.loop)

            # Check for global handler
            for handler in list(self.handlers[eventtype]['_']):
                if callable(handler):
                    asyncio.run_coroutine_threadsafe(handler(event), self.loop)
コード例 #14
0
ファイル: util.py プロジェクト: ahmedbodi/electrum
 def __init__(self, network: 'Network'):
     asyncio.set_event_loop(network.asyncio_loop)
     self.network = network
     self.interface = None  # type: Interface
     self._restart_lock = asyncio.Lock()
     self._reset()
     asyncio.run_coroutine_threadsafe(self._restart(), network.asyncio_loop)
     network.register_callback(self._restart, ['default_server_changed'])
コード例 #15
0
ファイル: main.py プロジェクト: UltrosBot/Ultros3K
    def _sigint(self, *_):
        """
        Handle CTRL+C or SIGINT and shut down gracefully.
        """

        print("")  # Or it'll print on the same line as the ^C
        self.log.debug("SIGINT caught.")
        asyncio.run_coroutine_threadsafe(self.shutdown(), self.event_loop)
コード例 #16
0
ファイル: stream.py プロジェクト: jsmitchell/sawtooth-core
 def put_message(self, message):
     """
     :param message: protobuf generated validator_pb2.Message
     """
     with self._condition:
         self._condition.wait_for(lambda: self._event_loop is not None)
     asyncio.run_coroutine_threadsafe(self._put_message(message),
                                      self._event_loop)
コード例 #17
0
 def send_message(self, msg, identity=None):
     """
     :param msg: protobuf validator_pb2.Message
     """
     with self._condition:
         self._condition.wait_for(lambda: self._event_loop is not None)
     asyncio.run_coroutine_threadsafe(self._send_message(identity, msg),
                                      self._event_loop)
コード例 #18
0
    def transmit_threadsafe(self, command_id, *args):
        """
        A thread safe version of send.

        :param command_id: Command id
        :param *args: Command arguments
        """
        asyncio.run_coroutine_threadsafe(self.transmit(command_id, *args), self.loop)
コード例 #19
0
ファイル: main.py プロジェクト: UltrosBot/Ultros3K
    def setup(self):
        """
        Set up this Ultros instance.

        This will parse configs, load plugins, and get networks connected. The managers however have already been
        instantiated.
        """
        self.network_manager.load_networks()
        asyncio.run_coroutine_threadsafe(self.network_manager.connect_all(), self.event_loop)
コード例 #20
0
ファイル: network.py プロジェクト: chrisrico/electrum
 def trigger_callback(self, event, *args):
     with self.callback_lock:
         callbacks = self.callbacks[event][:]
     for callback in callbacks:
         # FIXME: if callback throws, we will lose the traceback
         if asyncio.iscoroutinefunction(callback):
             asyncio.run_coroutine_threadsafe(callback(event, *args), self.asyncio_loop)
         else:
             self.asyncio_loop.call_soon_threadsafe(callback, event, *args)
コード例 #21
0
 def _push_chunk(self, chunk):
     if self._loop_thread.ident != get_ident():
         asyncio.run_coroutine_threadsafe(
             self._write_queue.put(chunk),
             loop=self._loop
         )
     else:
         # avoid races/hangs by just scheduling this, not using threadsafe
         self._loop.create_task(self._write_queue.put(chunk))
コード例 #22
0
ファイル: relay.py プロジェクト: Sorunome/OmnomIRC
	def stop(self):
		self.acceptMessages = False
		try:
			asyncio.run_coroutine_threadsafe(self.client.logout(), self.loop)
		except:
			pass
		try:
			asyncio.run_coroutine_threadsafe(self.client.close(), self.loop)
		except:
			pass
コード例 #23
0
ファイル: output.py プロジェクト: aarondewindt/urban-journey
    def flush_threadsafe(self, data):
        """
        Thread safe version of flush.

        :param data: Data to flush.
        """
        # ctlog.debug("OutputPort.flush_threadsafe({})".format(data))
        if self.channel is not None:
            loop = get_event_loop()
            asyncio.run_coroutine_threadsafe(self.channel.flush(data), loop)
コード例 #24
0
ファイル: master.py プロジェクト: jbremer/mitmproxy
 def shutdown(self):
     """
         Shut down the proxy. This method is thread-safe.
     """
     if not self.should_exit.is_set():
         self.should_exit.set()
         asyncio.run_coroutine_threadsafe(
             self._shutdown(),
             loop = self.channel.loop,
         )
コード例 #25
0
ファイル: api.py プロジェクト: TickerOfTime/snake
    def __init__(self, api, source_playlist:SpotifyPlaylist):
        self.api = api
        self.tracks = []
        self.playlist_id = source_playlist.id

        if len(source_playlist.tracks) > source_playlist.total_tracks:
            asyncio.run_coroutine_threadsafe(source_playlist.load_tracks())

        self.spotify_tracks = source_playlist.tracks
        self.playlist_hash = hash(source_playlist)
コード例 #26
0
ファイル: main.py プロジェクト: UltrosBot/Ultros3K
    def _sigbreak(self, *_):
        """
        Handle SIGBREAK (CTRL+BREAK or closing the CMD window) on Windows and shut down gracefully.

        Note that we only get five seconds when this happens before the process is killed, so we should try
        to keep this process fast.
        """

        self.log.debug("SIGBREAK caught.")
        asyncio.run_coroutine_threadsafe(self.shutdown(), self.event_loop)
コード例 #27
0
ファイル: asyncio.py プロジェクト: wwqgtxx/wwqLyParse
    def _cb():
        try:
            callback(*args, **kwargs)
        finally:
            _main_async_loop.stop()

            async def _null():
                pass

            asyncio.run_coroutine_threadsafe(_null(), _main_async_loop)
コード例 #28
0
ファイル: perf_processes.py プロジェクト: dougives/indy-node
def run_client(name, genesis_path, pipe_conn, seed, batch_size, batch_timeout, req_kind, bg_tasks, refresh):
    cln = LoadClient(name, pipe_conn, batch_size, batch_timeout, req_kind, bg_tasks, refresh)
    try:
        asyncio.run_coroutine_threadsafe(cln.run_test(genesis_path, seed), loop=cln._loop)
        cln._loop.run_forever()
    except Exception as e:
        print("{} running error {}".format(cln._name, e))
    # cln._loop.close()
    stat = cln._stat.dump_stat(dump_all=True)
    return stat
コード例 #29
0
ファイル: pyircbot.py プロジェクト: dpedu/pyircbot3
    def kill(self, message="Help! Another thread is killing me :("):
        """Shut down the bot violently

        :param sys_exit: True causes sys.exit(0) to be called
        :type sys_exit: bool
        :param message: Quit message
        :type message: str
        """
        self.closeAllModules()
        asyncio.run_coroutine_threadsafe(self.irc.kill(message=message), self.loop)
コード例 #30
0
 def test_simple_input_output_ports(self):
     ujml_code = '<?xml version="1.0"?><ujml version="{}">'.format(uj_version) + '''
                    <f_stoff s="s"/>
                 </ujml>'''
     s = Semaphore(0)
     globs = {"s": s}
     ujml = from_string(ujml_code, globals=globs)
     loop = get_event_loop()
     asyncio.run_coroutine_threadsafe(ujml[0].transmit(), loop=loop)
     assert s.acquire(timeout=0.1)
コード例 #31
0
 def sendData(self, data):
     for websocket in self.connected.copy():
         #print("Sending data: %s" % data)
         coro = yield from websocket.send(data)
         future = asyncio.run_coroutine_threadsafe(coro, loop)
コード例 #32
0
 def update_tracked_entity_ids(self, entity_ids):
     """Update the member entity IDs."""
     asyncio.run_coroutine_threadsafe(
         self.async_update_tracked_entity_ids(entity_ids),
         self.hass.loop).result()
コード例 #33
0
 async def next_song(self, ctx, loop):
     if len(self.queue[str(ctx.guild.id)]) is 0:
         await ctx.voice_client.disconnect()
         await ctx.send(
             "No songs are left in the queue... Just queue the 🍌 song.")
     #player = await YTDLSource.from_url(self.queue[str(ctx.guild.id)][0], loop=loop)
     player = self.queue[str(ctx.guild.id)][0]
     ctx.voice_client.play(
         player,
         after=lambda e: asyncio.run_coroutine_threadsafe(
             self.next_song(ctx, loop), loop=self.bot.loop).result())
     self.queue[str(ctx.guild.id)].remove(self.queue[str(ctx.guild.id)][0])
     em = discord.Embed(color=discord.Color(value=0x00ff00),
                        title=f"Playing")
     em.description = player.title
     duration = player.get_duration()
     em.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
     minutes, seconds = divmod(duration, 60)
     em.add_field(
         name='Length',
         value=
         f"{str(minutes)}:{str(seconds).replace('0', '00').replace('1', '01').replace('2', '02').replace('3', '03').replace('4', '04').replace('5', '05').replace('6', '06').replace('7', '07').replace('8', '08').replace('9', '09')}"
     )
     em.add_field(name='Volume', value=player.volume)
     em.add_field(name='Position in Queue',
                  value=len(self.queue[str(ctx.guild.id)]))
     msg = await ctx.send(embed=em)
     try:
         await msg.add_reaction("\U000023f8")  # Pause
         await msg.add_reaction("\U000025b6")  # Play
         await msg.add_reaction("\U000023f9")  # Stop
         await msg.add_reaction("\U0001f501")  # Repeat
         await msg.add_reaction("\U00002753")  # Help
     except discord.Forbidden:
         return await ctx.send(
             "I don't have Add Reaction permissions, so I can't show my awesome playing panel!"
         )
     try:
         while ctx.voice_client.is_playing():
             reaction, user = await self.bot.wait_for(
                 'reaction_add',
                 check=lambda reaction, user: user == ctx.author)
             if reaction.emoji == "⏸":
                 ctx.voice_client.pause()
                 await msg.remove_reaction("\U000023f8", ctx.author)
             elif reaction.emoji == "▶":
                 ctx.voice_client.resume()
                 await msg.remove_reaction("\U000025b6", ctx.author)
             elif reaction.emoji == "⏹":
                 ctx.voice_client.stop()
                 await msg.delete()
             elif reaction.emoji == "🔁":
                 ctx.voice_client.stop()
                 ctx.voice_client.play(
                     next,
                     after=lambda e: print('Player error: %s' % e)
                     if e else None)
                 await msg.remove_reaction("\U0001f501", ctx.author)
             elif reaction.emoji == "❓":
                 await msg.remove_reaction("\U00002753", ctx.author)
                 embed = discord.Embed(color=discord.Color(value=0x00ff00),
                                       title='Music Player Help')
                 embed.description = "**What do these magical buttons do?** \n\n:pause_button: Pauses the current song.\n:arrow_forward: Resumes any currently paused song.\n:stop_button: Stops the playing song and deletes this message.\n:repeat: Starts the current song from the beginning.\n:question: Shows this message."
                 embed.set_footer(
                     text='This will revert back in 15 seconds.')
                 await msg.edit(embed=embed)
                 await asyncio.sleep(15)
                 await msg.edit(embed=em)
     except discord.Forbidden:
         return await ctx.send("I can't remove your reactions! Ouch.")
コード例 #34
0
ファイル: player.py プロジェクト: polarisforsure/bolb-bot
 def _speak(self, speaking):
     try:
         asyncio.run_coroutine_threadsafe(self.client.ws.speak(speaking),
                                          self.client.loop)
     except Exception as e:
         log.info("Speaking call in player failed: %s", e)
コード例 #35
0
def run_async_from_thread(func: Callable[..., Coroutine[Any, Any, T_Retval]],
                          *args) -> T_Retval:
    f = asyncio.run_coroutine_threadsafe(
        func(*args), _local.loop)  # type: concurrent.futures.Future[T_Retval]
    return f.result()
コード例 #36
0
#!/usr/bin/env python3
import json
import asyncio
from statistics import median
from numbers import Number

from electrum_ltc.network import filter_protocol, Network
from electrum_ltc.util import create_and_start_event_loop, log_exceptions

loop, stopping_fut, loop_thread = create_and_start_event_loop()
network = Network()
network.start()


@log_exceptions
async def f():
    try:
        peers = await network.get_peers()
        peers = filter_protocol(peers)
        results = await network.send_multiple_requests(
            peers, 'blockchain.estimatefee', [2])
        print(json.dumps(results, indent=4))
        feerate_estimates = filter(lambda x: isinstance(x, Number),
                                   results.values())
        print(f"median feerate: {median(feerate_estimates)}")
    finally:
        stopping_fut.set_result(1)


asyncio.run_coroutine_threadsafe(f(), loop)
コード例 #37
0
 def _run_coro_threadsafe_with_tracing(self, coro):
     """Runs `coro` on `self._event_loop` inside the current trace spans."""
     with tracing.with_trace_context_from_rpc():
         return asyncio.run_coroutine_threadsafe(
             tracing.wrap_coroutine_in_current_trace_context(coro),
             self._event_loop)
コード例 #38
0
 def update_devices(event_time):
     asyncio.run_coroutine_threadsafe( hass.data[DOMAIN].async_update(), hass.loop)
コード例 #39
0
 def push_update(self, state):
     """Signal a state change and call the async counterpart."""
     asyncio.run_coroutine_threadsafe(self.async_push_update(state),
                                      self.hass.loop)
コード例 #40
0
 def add_token(self, token: 'Token'):
     asyncio.run_coroutine_threadsafe(self._add_token_key(token.get_key()),
                                      self.asyncio_loop)
コード例 #41
0
 def add(self, addr):
     asyncio.run_coroutine_threadsafe(self._add_address(addr),
                                      self.asyncio_loop)
コード例 #42
0
 def disconnect(self):
     asyncio.run_coroutine_threadsafe(self._disconnect(),self.loop)
     
コード例 #43
0
ファイル: sync.py プロジェクト: su600/opcua-asyncio
 def post(self, coro):
     if not self.loop or not self.loop.is_running():
         raise ThreadLoopNotRunning(f"could not post {coro}")
     futur = asyncio.run_coroutine_threadsafe(coro, loop=self.loop)
     return futur.result()
コード例 #44
0
 def _on_command(self, msg):
     """ProtocolRunner command listener"""
     # Notify command on main thread
     asyncio.run_coroutine_threadsafe(self._listener.on_protocol_event(msg),
                                      self._loop)
コード例 #45
0
ファイル: lnrater.py プロジェクト: qtumproject/qtum-electrum
 def maybe_analyze_graph(self):
     loop = asyncio.get_event_loop()
     fut = asyncio.run_coroutine_threadsafe(self._maybe_analyze_graph(),
                                            loop)
     fut.result()
コード例 #46
0
def on_stream_update(data):
    for stream in server.streams:
        if stream.status == 'playing':
            for group in server.groups:
                asyncio.run_coroutine_threadsafe(
                    group.set_stream(stream.identifier), loop)
コード例 #47
0
def run(awaitable):
    if should_use_sync():
        loop = get_or_create_background_event_loop()
        future = asyncio.run_coroutine_threadsafe(awaitable, loop)
        return future.result()
    return awaitable
コード例 #48
0
 def pull(self, wallet, force):
     return asyncio.run_coroutine_threadsafe(
         self.pull_thread(wallet, force),
         wallet.network.asyncio_loop).result()
コード例 #49
0
 def reply(self, channel, msg):
     asyncio.run_coroutine_threadsafe(
         self.dclient.send_message(channel, msg), self.dclient.loop)
コード例 #50
0
    def _on_message(self, client, userdata, msg):
        # NOTE! This method is called by the paho-mqtt thread, thus any invocation to the
        # asyncio platform must be scheduled via `self._loop.call_soon_threadsafe()` method.
        _LOGGER.debug(
            f"Received message from topic {msg.topic}: {str(msg.payload)}")

        # In order to correctly dispatch a message, we should look at:
        # - message destination topic
        # - message methods
        # - source device (from value in header)
        # Based on the network capture of Meross Devices, we know that there are 4 kinds of messages:
        # 1. COMMANDS sent from the app to the device (/appliance/<uuid>/subscribe) topic.
        #    Such commands have "from" header populated with "/app/<userid>-<appuuid>/subscribe" as that tells the
        #    device where to send its command ACK. Valid methods are GET/SET
        # 2. COMMAND-ACKS, which are sent back from the device to the app requesting the command execution on the
        #    "/app/<userid>-<appuuid>/subscribe" topic. Valid methods are GETACK/SETACK/ERROR
        # 3. PUSH notifications, which are sent to the "/app/46884/subscribe" topic from the device (which populates
        #    the from header with its topic /appliance/<uuid>/subscribe). In this case, only the PUSH
        #    method is allowed.
        # Case 1 is not of our interest, as we don't want to get notified when the device receives the command.
        # Instead we care about case 2 to acknowledge commands from devices and case 3, triggered when another app
        # has successfully changed the state of some device on the network.

        # Let's parse the message
        message = json.loads(str(msg.payload, "utf8"))
        header = message['header']
        if not verify_message_signature(header, self._cloud_creds.key):
            _LOGGER.error(
                f"Invalid signature received. Message will be discarded. Message: {msg.payload}"
            )
            return

        _LOGGER.debug("Message signature OK")

        # Let's retrieve the destination topic, message method and source party:
        destination_topic = msg.topic
        message_method = header.get('method')
        source_topic = header.get('from')

        # Dispatch the message.
        # Check case 2: COMMAND_ACKS. In this case, we don't check the source topic address, as we trust it's
        # originated by a device on this network that we contacted previously.
        if destination_topic == build_client_response_topic(self._cloud_creds.user_id, self._app_id) and \
                message_method in ['SETACK', 'GETACK', 'ERROR']:
            _LOGGER.debug(
                "This message is an ACK to a command this client has send.")

            # If the message is a PUSHACK/GETACK/ERROR, check if there is any pending command waiting for it and, if so,
            # resolve its future
            message_id = header.get('messageId')
            future = self._pending_messages_futures.get(message_id)
            if future is not None:
                _LOGGER.debug(
                    "Found a pending command waiting for response message")
                if message_method == 'ERROR':
                    err = CommandError(error_payload=message.payload)
                    self._loop.call_soon_threadsafe(_handle_future, future,
                                                    None, err)
                elif message_method in ('SETACK', 'GETACK'):
                    self._loop.call_soon_threadsafe(
                        _handle_future, future, message,
                        None)  # future.set_exception
                else:
                    _LOGGER.error(
                        f"Unhandled message method {message_method}. Please report it to the developer."
                        f"raw_msg: {msg}")
        # Check case 3: PUSH notification.
        # Again, here we don't check the source topic, we trust that's legitimate.
        elif destination_topic == build_client_user_topic(
                self._cloud_creds.user_id) and message_method == 'PUSH':
            namespace = header.get('namespace')
            payload = message.get('payload')
            origin_device_uuid = device_uuid_from_push_notification(
                source_topic)

            parsed_push_notification = parse_push_notification(
                namespace=namespace,
                message_payload=payload,
                originating_device_uuid=origin_device_uuid)
            if parsed_push_notification is None:
                _LOGGER.error(
                    "Push notification parsing failed. That message won't be dispatched."
                )
            else:
                asyncio.run_coroutine_threadsafe(
                    self._handle_and_dispatch_push_notification(
                        parsed_push_notification), self._loop)
        else:
            _LOGGER.warning(
                f"The current implementation of this library does not handle messages received on topic "
                f"({destination_topic}) and when the message method is {message_method}. "
                "If you see this message many times, it means Meross has changed the way its protocol "
                "works. Contact the developer if that happens!")
コード例 #51
0
 def push(self, wallet):
     return asyncio.run_coroutine_threadsafe(
         self.push_thread(wallet), wallet.network.asyncio_loop).result()
コード例 #52
0
ファイル: watch_address.py プロジェクト: ivansib/electrum-sib
    sys.exit(1)

# start network
loop = create_and_start_event_loop()[0]
network = Network()
network.start()


class Notifier(SynchronizerBase):
    def __init__(self, network):
        SynchronizerBase.__init__(self, network)
        self.watched_addresses = set()
        self.watch_queue = asyncio.Queue()

    async def main(self):
        # resend existing subscriptions if we were restarted
        for addr in self.watched_addresses:
            await self._add_address(addr)
        # main loop
        while True:
            addr = await self.watch_queue.get()
            self.watched_addresses.add(addr)
            await self._add_address(addr)

    async def _on_address_status(self, addr, status):
        print_msg(f"addr {addr}, status {status}")


notifier = Notifier(network)
asyncio.run_coroutine_threadsafe(notifier.watch_queue.put(addr), loop)
コード例 #53
0
 def broadcast_transaction_from_non_network_thread(self, tx, timeout=10):
     # note: calling this from the network thread will deadlock it
     fut = asyncio.run_coroutine_threadsafe(
         self.broadcast_transaction(tx, timeout=timeout), self.asyncio_loop)
     return fut.result()
コード例 #54
0
def get_transaction(tx):
    fut = asyncio.run_coroutine_threadsafe(get_tx_async(tx),
                                           network.asyncio_loop)
    return fut.result()
コード例 #55
0
 def stop(self):
     asyncio.run_coroutine_threadsafe(
         self.main_taskgroup.cancel_remaining(), self.asyncio_loop)
コード例 #56
0
 def send_pm(self, user, msg):
     asyncio.run_coroutine_threadsafe(
         self.dclient.start_private_message(user), self.dclient.loop)
     asyncio.run_coroutine_threadsafe(self.dclient.send_message(user, msg),
                                      self.dclient.loop)
コード例 #57
0
 def _stop_server_queue(self):
     # Get a new queue - no old pending connections thanks!
     self.server_queue = None
     asyncio.run_coroutine_threadsafe(
         self.server_queue_group.cancel_remaining(), self.asyncio_loop)
     self.server_queue_group = None
コード例 #58
0
ファイル: toy_gui.py プロジェクト: jamesqh/wt_stat_ocr
def doTask():
    asyncio.run_coroutine_threadsafe(updateLabel(), loop)
コード例 #59
0
    def __init__(
        self,
        url,
        language="en",
        proxy=None,
        find_redirect=False,
        api_url=None,
        debug=False,
        newParams=False,
        executablePath=None,
    ):
        self.url = url
        self.debug = debug
        self.proxy = proxy
        self.api_url = api_url
        self.referrer = "https://www.tiktok.com/"
        self.language = language
        self.executablePath = executablePath

        self.userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"
        self.args = [
            "--no-sandbox",
            "--disable-setuid-sandbox",
            "--disable-infobars",
            "--window-position=0,0",
            "--ignore-certifcate-errors",
            "--ignore-certifcate-errors-spki-list",
            "--user-agent=" + self.userAgent,
        ]

        if proxy != None:
            if "@" in proxy:
                self.args.append(
                    "--proxy-server="
                    + proxy.split(":")[0]
                    + "://"
                    + proxy.split("://")[1].split(":")[1].split("@")[1]
                    + ":"
                    + proxy.split("://")[1].split(":")[2]
                )
            else:
                self.args.append("--proxy-server=" + proxy)
        self.options = {
            "args": self.args,
            "headless": True,
            "ignoreHTTPSErrors": True,
            "userDataDir": "./tmp",
            "handleSIGINT": False,
            "handleSIGTERM": False,
            "handleSIGHUP": False,
        }

        if self.executablePath != None:
            self.options["executablePath"] = self.executablePath

        if async_support:
            loop = asyncio.new_event_loop()
            t = Thread(target=self.__start_background_loop, args=(loop,), daemon=True)
            t.start()
            if find_redirect:
                fut = asyncio.run_coroutine_threadsafe(self.find_redirect(), loop)
            elif newParams:
                fut = asyncio.run_coroutine_threadsafe(self.newParams(), loop)
            else:
                fut = asyncio.run_coroutine_threadsafe(self.start(), loop)
            fut.result()
        else:
            try:
                self.loop = asyncio.new_event_loop()
                if find_redirect:
                    self.loop.run_until_complete(self.find_redirect())
                elif newParams:
                    self.loop.run_until_complete(self.newParams())
                else:
                    self.loop.run_until_complete(self.start())
            except:
                self.loop.close()
コード例 #60
0
    async def play(self, ctx, *, url):
        """Search for a YouTube video to play, by name."""
        if ctx.author.voice is None:
            return await ctx.send(
                "Looks like you aren't connected to a voice channel yet! Where do I join?"
            )
        if ctx.voice_client is None:
            await ctx.author.voice.channel.connect()
        em = discord.Embed(
            color=discord.Color(value=0x00ff00),
            title="Searching...",
            description=
            f"{self.bot.get_emoji(441385713091477504)} Searching `{url}`...")
        m = await ctx.send(embed=em)
        if not ctx.voice_client.is_playing():
            try:
                player = await YTDLSource.from_url(url, loop=self.bot.loop)
            except youtube_dl.DownloadError:
                return await ctx.send(
                    "Couldn't find any video with that name. Try something else."
                )
            try:
                ctx.voice_client.play(
                    player,
                    after=lambda e: asyncio.run_coroutine_threadsafe(
                        self.next_song(ctx, self.bot.loop
                                       ), loop=self.bot.loop).result())
            except discord.Forbidden:
                return await ctx.send(
                    "I don't have permissions to play in this channel.")
            await m.delete()
            em = discord.Embed(color=discord.Color(value=0x00ff00),
                               title=f"Playing")
            em.description = f"**{player.title}**"
            em.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
            duration = player.get_duration()
            minutes, seconds = divmod(duration, 60)
            em.add_field(
                name='Length',
                value=
                f"{str(minutes)}:{str(seconds).replace('0', '00').replace('1', '01').replace('2', '02').replace('3', '03').replace('4', '04').replace('5', '05').replace('6', '06').replace('7', '07').replace('8', '08').replace('9', '09')}"
            )
            em.add_field(name='Volume', value=player.volume)
            em.add_field(name='Position in Queue', value='0')
            msg = await ctx.send(embed=em)
            try:
                await msg.add_reaction("\U000023f8")  # Pause
                await msg.add_reaction("\U000025b6")  # Play
                await msg.add_reaction("\U000023f9")  # Stop
                await msg.add_reaction("\U0001f501")  # Repeat
                await msg.add_reaction("\U00002753")  # Help
            except discord.Forbidden:
                return await ctx.send(
                    "I don't have Add Reaction permissions, so I can't show my awesome playing panel!"
                )
            try:
                while ctx.voice_client.is_playing():
                    reaction, user = await self.bot.wait_for(
                        'reaction_add',
                        check=lambda reaction, user: user == ctx.author)
                    if reaction.emoji == "⏸":
                        ctx.voice_client.pause()
                        await msg.remove_reaction("\U000023f8", ctx.author)
                    elif reaction.emoji == "▶":
                        ctx.voice_client.resume()
                        await msg.remove_reaction("\U000025b6", ctx.author)
                    elif reaction.emoji == "⏹":
                        ctx.voice_client.stop()
                        await msg.delete()
                    elif reaction.emoji == "🔁":
                        ctx.voice_client.stop()
                        ctx.voice_client.play(
                            player,
                            after=lambda e: print('Player error: %s' % e)
                            if e else None)
                        await msg.remove_reaction("\U0001f501", ctx.author)
                    elif reaction.emoji == "❓":
                        await msg.remove_reaction("\U00002753", ctx.author)
                        embed = discord.Embed(
                            color=discord.Color(value=0x00ff00),
                            title='Music Player Help')
                        embed.description = "**What do these magical buttons do?** \n\n:pause_button: Pauses the current song.\n:arrow_forward: Resumes any currently paused song.\n:stop_button: Stops the playing song and deletes this message.\n:repeat: Starts the current song from the beginning.\n:question: Shows this message."
                        embed.set_footer(
                            text='This will revert back in 15 seconds.')
                        await msg.edit(embed=embed)
                        await asyncio.sleep(15)
                        await msg.edit(embed=em)
            except discord.Forbidden:
                return await ctx.send("I can't remove your reactions! Ouch.")
            # except Exception as e:
            #     return await ctx.send(f"An unknown error occured. Details: \n\n```{e}```")

            # This made shit way too spammy, can't think of a good way to avoid it, rather just remove it.
        else:
            try:
                to_play = await YTDLSource.from_url(url, loop=self.bot.loop)
            except youtube_dl.DownloadError:
                return await ctx.send(
                    "Couldn't find any video with that name. Try something else."
                )
            try:
                self.queue[str(ctx.guild.id)].append(to_play)
            except KeyError:
                self.queue[str(ctx.guild.id)] = [to_play]
            em = discord.Embed(color=discord.Color(value=0x00ff00),
                               title='Added to queue!')
            em.description = f"**{to_play.title}**"
            em.add_field(name='Position in Queue',
                         value=len(self.queue[str(ctx.guild.id)]))
            duration = to_play.get_duration()
            minutes, seconds = divmod(duration, 60)
            em.add_field(
                name='Length',
                value=
                f"{str(minutes)}:{str(seconds).replace('0', '00').replace('1', '01').replace('2', '02').replace('3', '03').replace('4', '04').replace('5', '05').replace('6', '06').replace('7', '07').replace('8', '08').replace('9', '09')}"
            )
            em.set_author(name=f"Played by: {ctx.author.name}",
                          icon_url=ctx.author.avatar_url)
            await ctx.send(embed=em)