def test_published_messages(self) -> None: config = Config(LABBY_CONFIG_YAML) sequence = ExperimentSequence("./sequences/seq.yaml", SEQUENCE_YAML) runner = ExperimentRunner(config, sequence) with Sub0(dial=runner.subscription_address) as sub: sub.subscribe(b"") received_messages: List[ExperimentSequenceStatus] = [] with patch_time("2020-08-08"), patch_file_contents( "output/seq/000.csv"), patch("os.makedirs"): runner.start() while True: msg = sub.recv() status = ExperimentSequenceStatus.from_msgpack(msg) received_messages.append(status) if status.is_finished(): break runner.join() self.assertEqual( received_messages, [ ExperimentSequenceStatus(experiments=[ ExperimentStatus( name="000", state=ExperimentState.RUNNING, progress=0.0, ) ]), ExperimentSequenceStatus(experiments=[ ExperimentStatus( name="000", state=ExperimentState.RUNNING, progress=0.0, ) ]), ExperimentSequenceStatus(experiments=[ ExperimentStatus( name="000", state=ExperimentState.RUNNING, progress=0.5, ) ]), ExperimentSequenceStatus(experiments=[ ExperimentStatus( name="000", state=ExperimentState.RUNNING, progress=1.0, ) ]), ExperimentSequenceStatus(experiments=[ ExperimentStatus( name="000", state=ExperimentState.FINISHED, progress=1.0, ) ]), ], )
def subclient(): sub1 = Sub0(dial=address) sub1.subscribe(b'') i = 1 while True: msg=sub1.recv() print('收到的内容',msg)
async def subclientasyn(): sub1 = Sub0(dial=address) sub1.subscribe(b'') i = 1 while True: i = i + 1 msg= await sub1.arecv() print('收到的内容',msg)
def client_sub(): sub_sock = Sub0(dial=address) sub_sock.subscribe(b'') # sub_sock.subscribe(b'i') while True: print('we are receiving') print(sub_sock.recv())
def main(backend_url, nursery_url, database, user, password): print("Starting flappybird worker") connection_string = ( f"dbname='{database}' user='******' host='localhost' password='******'" ) with Sub0(dial=backend_url) as socket: socket.subscribe(b"") # Subscribe to everything trio.run(parent, socket, connection_string, nursery_url)
async def proxy_subpub(): sub_sock = Sub0(listen=addresssub) sub_sock.subscribe(b'') pub_sock = Pub0(listen=addresspub) while True: print('we are receiving') msg = sub_sock.recv() print(msg) pub_sock.send(msg)
def run(self) -> None: with Sub0(dial=self.subscription_address) as sub: sub.subscribe(b"") self.has_started = True for index, experiment in enumerate(self.sequence.experiments): while True: msg = sub.recv() sequence_status = ExperimentSequenceStatus.from_msgpack(msg) self.server.set_experiment_sequence_status(sequence_status) if sequence_status.experiments[index].is_finished(): break
def sat_validation(): """validate satellite info""" sat_servers = wxcutils.load_json(CONFIG_PATH, 'sat-servers.json') result = [] for sat_server in sat_servers: MY_LOGGER.debug('Processing %s', sat_server) address = 'tcp://' + sat_server['ip'] + ':' + sat_server['port'] sub0 = Sub0(dial=address, recv_timeout=100, topics="") # make sure everyone is connected time.sleep(0.1) retry_counter = 1 op = 'unset' while retry_counter <= 10: try: op = json.loads(sub0.recv().decode("utf-8")) result.append({ 'timestamp': op['timestamp'], 'skipped_symbols': op['skipped_symbols'], 'viterbi_errors': op['viterbi_errors'], 'reed_solomon_errors': op['reed_solomon_errors'], 'ok': 'Locked' if op['ok'] else 'Unlocked', 'label': sat_server['label'], 'when': str(time.time()) }) break except: MY_LOGGER.debug('Attempt %d', retry_counter) MY_LOGGER.debug( 'Unexpected error connecting to %s : 0 %s 1 %s 2 %s', address, sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) retry_counter += 1 MY_LOGGER.debug('Sleeping 2 seconds...') time.sleep(2) MY_LOGGER.debug('op %s', op) MY_LOGGER.debug('result = %s', result) # close the socket now we've finished with it sub0.close() wxcutils.save_json(OUTPUT_PATH, 'satellite-receivers.json', result)
async def save_to_database(nursery_url, conn, partition_size=100): with Sub0(dial=nursery_url) as sub: sub.subscribe(b"") # Subscribe to everything stream = Stream(asynchronous=False) ( stream.map(ujson.loads) .flatten() .map(flatten_record) .partition(partition_size) .map(pd.DataFrame) .sink(partial(insert_dataframe, "logs", conn)) ) while True: stream.emit(await sub.arecv())
def main(): with Pub0(listen=avionics_address) as avi_pub, \ Sub0(dial=altitude_address, recv_timeout=300) as sub0: #pubsub obj constructors sub0.subscribe(b'altitude') #sub to the altitude topic sub0.subscribe(b'avionics') #sub to the avionics topic sub0.recv_buffer_size = 128 time.sleep(0.05) for _ in range(10): avi_pub.send(b'avionics:' + data_to_send()) try: mes_rec.append(sub0.recv()) except Timeout: print('Timeout, no message recieved') time.sleep(0.25) extract_data(mes_rec, "altitude:")
def realtime_multidata(self): sub1 = Sub0(dial=address) sub1.subscribe(b'sin') print('we are at realtine') # 突然想到还是采用多个pub 多个sub 以及 中间的代理部分 # 如何启动这些内容 # 如果多个不同的地方分布到不同的前台的界面,相应速度是否会收到影响。 while True: msg = sub1.recv() msg=msg.decode() name,data=msg.split('+') jsondata={ name:data.split('=') #这个是启用多个数据一起发送的方案 } a = json.dumps(jsondata) print(type(a)) print(a) self.send(str(a))
def chat_message(self): sub1 = Sub0(dial=address) sub1.subscribe(b'') print('we are at chatmsg') # 突然想到还是采用多个pub 多个sub 以及 中间的代理部分 # 如何启动这些内容 # 如果多个不同的地方分布到不同的前台的界面,相应速度是否会收到影响。 while True: msg = sub1.recv() msg=msg.decode() name,data=msg.split('+') jsondata={ name:data.split(',') } a = json.dumps(jsondata) print(type(a)) # print(jsondata['sin']) self.send(str(a))
async def model_flap(nursery_url, conn, partition_size=100): with Sub0(dial=nursery_url) as sub: sub.subscribe(b"") stream = Stream(asynchronous=False) ( stream.map(ujson.loads) .flatten() .map(flatten_record) .partition(partition_size) .map(pd.DataFrame) .map(build_game_table) .map(partial(model_train, None, conn)) .map(partial(model_test, None, conn)) .sink(print) ) while True: stream.emit(await sub.arecv())
import time from pynng import Pub0, Sub0, Timeout address = 'tcp://127.0.0.1:31313' with Pub0(listen=address) as pub, \ Sub0(dial=address, recv_timeout=100) as sub0, \ Sub0(dial=address, recv_timeout=100) as sub1, \ Sub0(dial=address, recv_timeout=100) as sub2, \ Sub0(dial=address, recv_timeout=100) as sub3: sub0.subscribe(b'wolf') sub1.subscribe(b'puppy') # The empty string matches everything! sub2.subscribe(b'') # we're going to send two messages before receiving anything, and this is # the only socket that needs to receive both messages. sub2.recv_buffer_size = 2 # sub3 is not subscribed to anything # make sure everyone is connected time.sleep(0.05) pub.send(b'puppy: that is a cute dog') pub.send(b'wolf: that is a big dog') print(sub0.recv()) # prints b'wolf...' since that is the matching message print(sub1.recv()) # prints b'puppy...' since that is the matching message # sub2 will receive all messages (since empty string matches everything) print(sub2.recv()) # prints b'puppy...' since it was sent first print(sub2.recv()) # prints b'wolf...' since it was sent second
async def listening_data(self,subscribe_content): print('we are at linsting data') # await self.send_data2front() # t1 = threading.Thread(target=self.send_data2front) # topic= sub1 = Sub0(dial=address) # print(subscribe_content) # sub1.subscribe(subscribe_content) # sub1.subscribe(b'sawtooth') # subscribe_content=[] # sub1.subscribe(b'') # if len(subscribe_content)==0: sub1.subscribe(b'') else: for subtopic in subscribe_content: print(subtopic) sub1.subscribe(subtopic) print('we are have subscribe someting') # 突然想到还是采用多个pub 多个sub 以及 中间的代理部分 # 如何启动这些内容 # 如果多个不同的地方分布到不同的前台的界面,相应速度是否会收到影响。 ###发送多个数据 # while True: # msg =await sub1.arecv() # msg = msg.decode() # # self.send(msg) # name, data = msg.split('+') # jsondata = { # name: data.split('=') # 这个是启用多个数据一起发送的方案 # } # a = json.dumps(jsondata) # print('we are in d多个数据发送') # print(a) # # await self.send(a) # print('发送成功了 啊') ###发送单个数据 while True: print('we are at before sending') msg = await sub1.arecv() print('发送单个数据') msg=msg.decode() name,data=msg.split('+') jsondata={ name:data.split(',') #单个数据发送方案 } a = json.dumps(jsondata) print(a) await self.send(str(a))
def new_message(self, message): if message.from_user.username == VanBotSettings.username: try: msg = message.text.lower() except: print("Not a text message") msg = "" if msg == 'get': self.send_last_frame() elif msg.startswith('set alarm time'): try: t = int(msg.replace('set alarm time','')) for md in self.md: md.alarmTimeSeconds = t message.reply_text("Alarm time set: {0}".format(t)) except: message.reply_text("Command error") elif msg == 'alarm on': self.notify_alarm = True for md in self.md: md.alarm_on() message.reply_text("Alarm is On") elif msg == 'alarm off': self.notify_alarm = False for md in self.md: md.alarm_off() message.reply_text("Alarm is Off") elif msg == 'rec start': for md in self.md: filename = self.start_recording(md) if filename is not None: message.reply_text("Recording started: {0}".format(filename)) else: message.reply_text("Start recording FAILED") elif msg == 'rec stop': for md in self.md: filename = self.stop_recording(md) if filename is not None: message.reply_text("Recording stopped: {0}".format(filename)) else: message.reply_text("Stop recording FAILED") elif msg == 'rec send': if self.lastFilename is not None: print("Sending file: {0}".format(self.lastFilename)) message.reply_document(open(self.lastFilename,'rb'), filename=self.lastFilename) elif msg == 'gps': try: gpsd.connect() packet = gpsd.get_current() s = "{0}, {1}\nhttps://www.google.com/maps/search/?api=1&query={0},{1}".format(packet.position()[0],packet.position()[1]) message.reply_text(s) except: message.reply_text("Cannot get gps coordinates") elif msg == 'solar info': try: subSocket = Sub0(dial=VanBotSettings.solar_system['address_pub'], recv_timeout=1000) subSocket.subscribe(b'') try: data = subSocket.recv() data_str = data.decode().replace("solardata: ", "").replace("'", "\"") data = json.loads(data_str) reply_str = "" for d in data: reply_str += "{0}: {1}\n".format(d, data[d]) message.reply_text(reply_str) except Timeout: message.reply_text("Failed to get solar data, receive timeout") except Exception as e: message.reply_text("Failed to get solar data: {0}".format(e)) elif msg == 'help': s = "Available commands:\n" for cmd in self.commamds: s += "{0} - {1}\n".format(cmd['name'], cmd['description']) message.reply_text(s) else: message.reply_text("Unknown command")
from pynng import Pub0, Sub0 import time address = "ws://127.0.0.1:22315" # address="tcp://127.0.0.1:22315" sub = Sub0(dial=address) sub.subscribe(b'') i = 1 while True: i = i + 1 print('we are receiving') a = sub.recv() print('we are receiveing:', a)
from time import time import asyncio import signal from flexx import flx from solar_settings import SolarSettings from solar_data import SolarData from pynng import Pair1, Sub0, TryAgain #serial data in and out subSocket = Sub0(dial=SolarSettings.serial_pub_address) subSocket.subscribe("solardata") msgSocket = Pair1(dial=SolarSettings.serial_msg_address, polyamorous=True) class Relay(flx.Component): number_of_connections = flx.IntProp(settable=True) def init(self): self.update_number_of_connections() @flx.manager.reaction('connections_changed') def update_number_of_connections(self, *events): n = 0 for name in flx.manager.get_app_names(): sessions = flx.manager.get_connections(name) n += len(sessions) self.set_number_of_connections(n) @flx.emitter def new_data(self, serial_data): return dict(serial_data=serial_data,
from pynng import Sub0 from streamz import Stream import ujson from flappystream.analysis import flatten_record if __name__ == "__main__": with Sub0(dial="tcp://127.0.0.1:54321") as socket: print('Connecting...') socket.subscribe(b"") try: stream = Stream() stream.map(ujson.loads).flatten().map(flatten_record).sink(print) while True: stream.emit(socket.recv()) except KeyboardInterrupt: print("Bye")