def run(use_proxy, source): config_file = os.path.expanduser('~') + '/.hikyuu/hikyuu.ini' if not os.path.exists(config_file): print("未找到配置文件,请先运行 HikyuuTDX 进行配置与数据导入") exit(1) hikyuu_init(config_file, ignore_preload=True) print("采集程序运行中,可使用 Ctrl-C 终止!") sm = StockManager.instance() stk_list = [ stk.market_code.lower() for stk in sm if stk.valid and stk.type in (constant.STOCKTYPE_A, constant.STOCKTYPE_INDEX, constant.STOCKTYPE_GEM) ] def batch_func(records): buf = create_fb_spot(records) pub_sock.send(bytes(buf)) address = "ipc:///tmp/hikyuu_real_pub.ipc" pub_sock = pynng.Pub0() pub_sock.listen(address) try: while True: records = get_spot_parallel(stk_list, source, use_proxy, batch_func) print(len(records)) #pub_sock.send(date.encode()) time.sleep(3) except KeyboardInterrupt: exit(1)
def test_sockets_get_garbage_collected(): # from issue90 with pynng.Pub0() as _: pass _ = None gc.collect() objs = [o for o in gc.get_objects() if isinstance(o, pynng.Pub0)] assert len(objs) == 0
async def main(): with pynng.Pub0() as pub: n0 = await server(pub) async with curio.TaskGroup(wait=all) as g: await g.spawn(client, "client0", 2) await g.spawn(client, "client1", 3) await g.spawn(client, "client2", 4) await n0.cancel()
async def pub(): with pynng.Pub0(listen=addr) as pubber: for i in range(20): prefix = 'even' if is_even(i) else 'odd' msg = '{}:{}'.format(prefix, i) await pubber.asend(msg.encode('ascii')) while not all(sentinel_received.values()): # signal completion await pubber.asend(b'odd:None') await pubber.asend(b'even:None')
async def live(channel: str, tickers, **kwargs): """ Broadcasts live data feeds Args: channel: channel name tickers: list of tickers **kwargs: other parameters for `blp.live` """ with pynng.Pub0() as pub: pub.listen(address=f'{ADDRESS}/{channel}') async for data in blp.live(tickers=tickers, **kwargs): print(data) await pub.asend(orjson.dumps(data))
def test_sub_sock_options(): with pynng.Pub0(listen=addr) as pub: # test single option topic with pynng.Sub0(dial=addr, topics='beep', recv_timeout=500) as sub: wait_pipe_len(sub, 1) pub.send(b'beep hi') assert sub.recv() == b'beep hi' with pynng.Sub0(dial=addr, topics=['beep', 'hello'], recv_timeout=500) as sub: wait_pipe_len(sub, 1) pub.send(b'beep hi') assert sub.recv() == b'beep hi' pub.send(b'hello there') assert sub.recv() == b'hello there'
def test_pubsub0(): with pynng.Sub0(listen=addr, recv_timeout=100) as sub, \ pynng.Pub0(dial=addr, recv_timeout=100) as pub: sub.subscribe(b'') msg = b'i am requesting' time.sleep(0.04) pub.send(msg) assert sub.recv() == msg # TODO: when changing exceptions elsewhere, change here! # publishers can't recv with pytest.raises(pynng.NotSupported): pub.recv() # responders can't send before receiving with pytest.raises(pynng.NotSupported): sub.send(b"""I am a bold subscribing socket. I believe I was truly meant to be a publisher. The world needs to hear what I have to say! """)
#!/usr/local/bin/python3 """ nng Publisher """ import pynng import time #addr = "tcp://127.0.0.1:2345" addr = "tcp://192.168.0.15:23456" #with pynng.Sub0(listen=addr, recv_timeout=100) as sub: with pynng.Pub0(dial=addr, recv_timeout=30000) as pub: print('Publish message to subscriber (use quit to exit)') while True: line = input('message> ') pub.send(line.encode('utf-8')) if line == 'quit': break pub.close()
def run(use_proxy, source, seconds, phase1, phase2, ignore_weekend): phase1_delta = parse_phase(phase1) hku_error_if(phase1_delta is None or len(phase1_delta) != 2, "无效参数 phase1: {}".format(phase1), callback=lambda: exit(1)) hku_error_if(phase1_delta[0] > phase1_delta[1], "无效参数 phase1: {}, 结束时间应大于等于起始时间".format(phase1), callback=lambda: exit(1)) phase2_delta = parse_phase(phase2) hku_error_if(phase2_delta is None or len(phase2_delta) != 2, "无效参数 phase2: {}".format(phase2), callback=lambda: exit(1)) hku_error_if(phase2_delta[0] > phase2_delta[1], "无效参数 phase2: {}, 结束时间应大于等于起始时间".format(phase2), callback=lambda: exit(1)) hku_error_if( phase1_delta[1] > phase2_delta[0], "无效参数 phase1: {}, phase2: {}, phase2 起始时间应大于等于 phase1 结束时间".format( phase1, phase2), callback=lambda: exit(1)) hku_logger.info("采集时间段1:{}".format(phase1)) hku_logger.info("采集时间段2:{}".format(phase2)) config_file = os.path.expanduser('~') + '/.hikyuu/hikyuu.ini' if not os.path.exists(config_file): print("未找到配置文件,请先运行 HikyuuTDX 进行配置与数据导入") exit(1) hikyuu_init(config_file, ignore_preload=True) print("采集程序运行中,可使用 Ctrl-C 终止!") sm = StockManager.instance() stk_list = [ stk.market_code.lower() for stk in sm if stk.valid and stk.type in (constant.STOCKTYPE_A, constant.STOCKTYPE_INDEX, constant.STOCKTYPE_GEM) ] spot_topic = ':spot:' def batch_func(records): spot = bytearray(spot_topic.encode('utf-8')) buf = create_fb_spot(records) spot.extend(buf) pub_sock.send(bytes(spot)) address = "ipc:///tmp/hikyuu_real_pub.ipc" pub_sock = pynng.Pub0() pub_sock.listen(address) today = Datetime.today() phase1_time = [today + x for x in phase1_delta] phase2_time = [today + x for x in phase2_delta] start_time = Datetime.now() if not (phase1_time[0] <= start_time <= phase1_time[1] or phase2_time[0] <= start_time <= phase2_time[1]): delta = next_delta(start_time, seconds, phase1_delta, phase2_delta, ignore_weekend) next_time = start_time + delta hku_info("启动采集时间:{}".format(next_time)) time.sleep(delta.total_seconds()) while True: try: start_time = Datetime.now() pub_sock.send("{}{}".format(spot_topic, '[start spot]').encode('utf-8')) records = get_spot_parallel(stk_list, source, use_proxy, batch_func) hku_info("{}:{}:{} 采集数量: {}".format(start_time.hour, start_time.minute, start_time.second, len(records))) pub_sock.send('{}{}'.format(spot_topic, '[end spot]').encode('utf-8')) delta = next_delta(start_time, seconds, phase1_delta, phase2_delta, ignore_weekend) time.sleep(delta.total_seconds()) except Exception as e: hku_error(e) time.sleep(10)