to_ck/app.py
2021-05-11 16:19:56 +08:00

50 lines
1.6 KiB
Python

import time
from multiprocessing import Process
from settings import settings
from v2 import *
from v2.struct_cache import StructCacheFile, StructCacheRedis
class XProcess(Process):
def __init__(self, partition, lock, ipsearch):
super(XProcess, self).__init__()
self.partition = partition
self.lock = lock
self.ipsearch = ipsearch
def run(self):
db_client = CK(**settings.CK_CONFIG)
sketch = Sketch(db_client, StructCacheRedis())
handler_event = HandlerEvent(db_client, settings.GAME, self.ipsearch)
handler_user = HandlerUser(db_client, settings.GAME)
transmitter = Transmitter(db_client, settings.GAME, sketch, self.lock)
transmitter.add_source(handler_event, 10000, 60)
transmitter.add_source(handler_user, 1000, 60)
last_ts = int(time.time())
consumer = create_consumer(self.partition)
for topic, msg in consumer():
# print(msg)
type_ = msg['#type']
del msg['#type']
ts = int(time.time())
if 'user' in type_:
# continue
obj = getattr(handler_user, type_)
handler_user.receive_data.append(UserAct(obj, msg))
if len(handler_user.receive_data) >= 1000 or last_ts + 60 < ts:
last_ts = ts
handler_user.execute()
elif 'track' in type_:
# continue
obj = getattr(handler_event, type_)
obj(msg)
else:
continue
transmitter.run()