def thread_target(self, loop):
        asyncio.set_event_loop(loop)
        retry = True
        update = True
        sz = None
        sh = None
        while retry:
            try:
                [szTime, shTime] = self.sina.get_ticktime()
                if (szTime > self.szTime) & (len(self.szSymbols) > 0):
                    self.szTime = szTime
                    szUpdate = True
                else:
                    szUpdate = False

                if (shTime > self.shTime) & (len(self.shSymbols) > 0):
                    self.shTime = shTime
                    shUpdate = True
                else:
                    shUpdate = False
                if szUpdate and shUpdate:
                    [sz,
                     sh] = self.sina.get_realtime_quotes(symbols=self.symbols,
                                                         dataframe=True,
                                                         loop=loop,
                                                         split=True)
                elif szUpdate:
                    sz = self.sina.get_realtime_quotes(symbols=self.szSymbols,
                                                       dataframe=True,
                                                       loop=loop)
                elif shUpdate:
                    sh = self.sina.get_realtime_quotes(symbols=self.shSymbols,
                                                       dataframe=True,
                                                       loop=loop)
                retry = False
            except Exception as e:
                print(e)
        # print( time.time() - start )
        if sz is not None:
            eventSZ = Event(eventType='SinaFreeQuote', data=sz)
            eventSZ.time = szTime
            eventSZ.localtime = time.time()
            eventSZ.exchange = 'SZ'
            for q in self._subscriber:
                q.put(eventSZ)
        if sh is not None:
            eventSH = Event(eventType='SinaFreeQuote', data=sh)
            eventSH.time = shTime
            eventSH.localtime = time.time()
            eventSH.exchange = 'SH'
            for q in self._subscriber:
                q.put(eventSH)
    def create_ws(self, qlist, symbolList):
        retry = True
        while retry:
            try:
                response = yield from self.get_ws_token(qlist)
                if response["msg_code"] == 1:
                    token = response["result"]
                    self.logger.info(
                        "成功获取到token, symbolList = {}".format(symbolList))
                    retry = False
                else:
                    self.logger.warning("{},{}".format(response, qlist))
                    if response["msg_code"] == -11:
                        time.sleep(2)
                        self.logger.warning("尝试重新登录新浪")
                        self.sina = V("Sina")
                        self.sina.login(verify=False)

            except Exception as e:
                self.logger.warning(e)

        url_wss = 'wss://ff.sinajs.cn/wskt?token=' + token + '&list=' + qlist

        while True:  # 建立websocket连接
            try:
                ws = yield from websockets.connect(url_wss)
                self.websockets[symbolList[0]] = dict()
                self.websockets[symbolList[0]]["ws"] = ws
                self.websockets[symbolList[0]]["qlist"] = qlist
                self.websockets[symbolList[0]]["token"] = token
                self.websockets[symbolList[0]]["renewed"] = datetime.now()
                self.websockets[symbolList[0]]["trialTime"] = 0
                self.logger.info("成功建立ws连接, {}, symbolList = {}".format(
                    threading.current_thread().name, symbolList))
                break
            except Exception as e:
                self.logger.warning(
                    "重试 websockets.connect , {}, symbolList = {}".format(
                        threading.current_thread().name, symbolList))

        while self._active:
            try:
                message = yield from ws.recv()
                event = Event(event_type='SinaLevel2WS', data=message)

                for q in self._subscriber:
                    q.put(event)
                    # print( "Producer:\n{},{}".format( event.data,datetime.now() ) )

            except Exception as e:
                self.logger.error("{},{}".format(
                    e,
                    threading.current_thread().name))
                ws.close()
                yield from self.create_ws(qlist=qlist, symbolList=symbolList)
Beispiel #3
0
 def handler(self):
     # handler是需要被重写的方法,以下demo每隔0.5秒产生一个数据
     # 并且把数据推送给它的订阅者
     import time
     i = 0
     while (self._active):
         i += 1
         event = Event(event_type='这是Event.type',
                       data="这是Event.data:{}".format(i))
         print("DemoProducer:", event.data)
         for q in self._subscriber:
             q.put(event)
         time.sleep(0.5)
Beispiel #4
0
    def create_ws(self, qlist, symbol, loop):
        asyncio.set_event_loop(loop)
        retry = True
        while retry:
            try:
                token = yield from self.get_ws_token(qlist)
                retry = False
            except Exception as e:
                self.logger.warning(e)

        url_wss = 'wss://ff.sinajs.cn/wskt?token=' + token + '&list=' + qlist

        start = datetime.now()
        retry = True
        while retry:
            try:
                ws = yield from websockets.connect(url_wss)
                retry = False
            except Exception as e:
                self.logger.warning("重试 websockets.connect , %s " %
                                    threading.current_thread().name)

        # 另开一个线程每40秒更新一次token,新建一个event_loop防止这个操作阻塞websocket
        loopToken = asyncio.new_event_loop()
        tasks = [self.renew_token(ws, qlist, token)]
        renewToken = threading.Timer(40, util.thread_loop, (loopToken, tasks))
        renewToken.start()
        self.logger.info("开启线程:{} 为 {} 更新token".format(
            renewToken.name,
            threading.current_thread().name))

        while self._active:
            try:
                message = yield from ws.recv()
                # 如果要的不是原始数据
                if self.raw == False:
                    message = self.ws_parse(message=message)

                event = Event(eventType='SinaLevel2WS', data=message)

                for q in self._subscriber:
                    q.put(event)

            except Exception as e:
                self.logger.error("{},{}".format(
                    e,
                    threading.current_thread().name))
                ws.close()
                yield from self.create_ws(qlist=qlist,
                                          symbol=symbol,
                                          loop=loop)
	def thread_target(self, loop):
		asyncio.set_event_loop(loop)
		retry = True
		update = True
		sz = None
		sh = None
		while retry:
			try:
				[ szTime, shTime ] = self.sina.get_ticktime()
				if (szTime > self.szTime) & (len(self.szSymbols)>0):
					self.szTime = szTime
					szUpdate = True
				else:
					szUpdate = False

				if (shTime > self.shTime) & (len(self.shSymbols)>0):
					self.shTime = shTime
					shUpdate = True
				else:
					shUpdate = False
				if szUpdate and shUpdate:
					[ sz ,sh ] = self.sina.get_realtime_quotes( symbols = self.symbols , dataframe=True, loop = loop, split = True )
				elif szUpdate:
					sz = self.sina.get_realtime_quotes( symbols = self.szSymbols , dataframe=True, loop = loop )
				elif shUpdate:
					sh = self.sina.get_realtime_quotes( symbols = self.shSymbols , dataframe=True, loop = loop )
				retry = False
			except Exception as e:
				print(e)
		# print( time.time() - start )
		if sz is not None:
			eventSZ = Event( event_type = 'SinaFreeQuote', data = sz )
			eventSZ.time = szTime
			eventSZ.localtime = time.time()
			eventSZ.exchange = 'SZ'
			for q in self._subscriber:
				q.put(eventSZ)
		if sh is not None:
			eventSH = Event( event_type = 'SinaFreeQuote', data = sh )
			eventSH.time = shTime
			eventSH.localtime = time.time()
			eventSH.exchange = 'SH'
			for q in self._subscriber:
				q.put(eventSH)
Beispiel #6
0
    def create_ws(self, qlist, symbol, loop):
        asyncio.set_event_loop(loop)
        retry = True
        while retry:
            try:
                token = yield from self.get_ws_token(qlist)
                retry = False
            except Exception as e:
                print(e)

        url_wss = 'wss://ff.sinajs.cn/wskt?token=' + token + '&list=' + qlist

        start = datetime.now()
        retry = True
        while retry:
            try:
                ws = yield from websockets.connect(url_wss)
                retry = False
            except Exception as e:
                print("Retrying..")

        # 另开一个线程每40秒更新一次token,新建一个event_loop防止这个操作阻塞websocket
        loopToken = asyncio.new_event_loop()
        tasks = [self.renew_token(ws, qlist)]
        renewToken = threading.Timer(40, util.thread_loop, (loopToken, tasks))
        renewToken.start()

        while self._active:
            try:
                message = yield from ws.recv()
                # 如果要的不是原始数据
                if self.raw == False:
                    message = self.ws_parse(message=message)

                event = Event(eventType='SinaLevel2WS', data=message)

                for q in self._subscriber:
                    q.put(event)

            except Exception as e:
                print(e)
                ws.close()
                yield from self.create_ws(qlist=qlist,
                                          symbol=symbol,
                                          loop=loop)