def build_app(backend_address="tcp://127.0.0.1", backend_port=54321): # Stage 1 app = Starlette(debug=True) app.mount( "/static", app=StaticFiles(directory=Path(__file__).parent / "static"), name="static", ) s = Pub0(listen=f"{backend_address}:{backend_port}") @app.route("/") async def home(request): async with aiofiles.open( Path(__file__).parent / "static" / "index.html") as f: return Response(await f.read(), media_type="text/html") @app.route("/log", methods=["POST"]) async def log(request): # Send the message to the worker await s.asend(await request.body()) return Response("") return app
def pubserver(): pub = Pub0(dial=address) i = 1 while True: i = i + 1 time.sleep(1) print('we are sending-----') pub.send(b'asyn masg')
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)
async def open_comms(address, sec, mes): with Pub0(listen=address) as alt_pub: time.sleep(0.25) global next_msg for _ in range(50): next_msg = time.time() + sec alt_pub.send(b'altitude:' + data_to_send()) print(mes) print(time.time()) await asyncio.sleep(next_msg - time.time())
def __init__(self, settings): super().__init__() self.running = False self.settings = settings self.serial = None self.connected = False self.pubSocket = Pub0() self.msgSocket = Pair1(polyamorous=True) self.firstDataReceived = False self.lastDataTime = time.time()
def __init__(self, config: Config, sequence: ExperimentSequence) -> None: super().__init__() self.config = config self.sequence = sequence self.subscription_address = _ADDRESS self.pub = Pub0(listen=self.subscription_address) self.sequence_status = ExperimentSequenceStatus(experiments=[ ExperimentStatus( name=experiment.name, state=ExperimentState.NOT_STARTED, progress=0.0, ) for experiment in self.sequence.experiments ])
async def trianglepubserverasynori(): pub = Pub0(dial=address) z = 1 # timeinterval = 1 periodnum = 1 # x = np.around(np.arange((Z - 1) * 2 * np.pi, (Z - 1) * 2 * np.pi + 2 * np.pi, 0.01),decimals=2) i = 0 x, y = triangle_wave(start=(z - 1) * zhouqi, zhouqi=zhouqi, midu=glo_midu, xdecimals=2, ydecimals=5) while True: # await trio.sleep(1) await asyncio.sleep(timeinterval) print('we are sending triangle') #多个数据一起上传 # msg='' # for j in range (10): # msg = msg +str(x[i])+','+str(y[i])+'=' # i = i + 1 # if i >= 62: # i = 0 # Z = Z + 1 # print('z的大小', Z) # # x = np.around(np.arange((Z - 1) * 2 * np.pi, (Z - 1) * 2 * np.pi + 2 * np.pi, 0.1), decimals=2) # # y = np.around(np.sin(x) * 100, decimals=5) # # print(msg) # # print(data) # await pub.asend(msg.encode()) #单个数据独立上传 # print(data) await pub.asend(('triangle+' + str(x[i]) + ',' + str(y[i])).encode()) i = i + 1 if i >= 100: i = 0 z = z + 1 print('z的大小', z) x, y = triangle_wave(start=(z - 1) * zhouqi, zhouqi=zhouqi, midu=glo_midu, xdecimals=2, ydecimals=5)
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:")
async def pubserverasyn(): pub = Pub0(dial=address) db = pymysql.connect(host='localhost', user='******', password='******', db='test', port=3306, charset='utf8') cur = db.cursor() i = 0 while True: # await trio.sleep(1) await asyncio.sleep(0.1) print('we are reading--data---') sql = 'select * from sinvalue order by id DESC limit 1;' cur.execute(sql) # 选择需要读取的数据 db.commit( ) #db commit 确定提交这条记录 否则,每次执行的时候,返回都是第一次自信的结果。如果要获取最新的一条的数据,需要使用db.commit 对其进行提交 data = cur.fetchall() # 对读取到的数据赋值为data print(data) await pub.asend((str(data[0][1]) + ',' + str(data[0][2])).encode())
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
from pynng import Pub0 import time address = "ws://127.0.0.1:22315" # address = 'tcp://127.0.0.1:22315' pub = Pub0(listen=address) i = 1 while True: i = i + 1 time.sleep(1) print('we are sending-----') pub.send(b'asyn masg')
async def hub(socket, nursery_url): with Pub0(listen=nursery_url) as pub: while True: log = await socket.arecv() await pub.asend(log)