def run(self): self.started = True while self.started: try: topic, content = self.messsageQueue.get_nowait() message.pub(topic, content) except Queue.Empty, e: time.sleep(self.timeout)
def inner_wrapper(*args, **kwargs): pub_event = tuple(event.split('.')) if pub_event[-1] == 'before': message.pub(pub_event, *args, **kwargs) result = func(*args, **kwargs) if pub_event[-1] == 'after': message.pub(pub_event, *args, **kwargs) return result
def hello(name): print "hello, %s."%name def hi(name): print "hi, %s."%name def stop(name): print 'hello, %s. greet5'%name print 'discontinued.' ctx = message.Context() ctx.discontinued = True return ctx #订阅一个话题,并发布 message.sub('greet', hello) message.sub('greet', hi) message.pub('greet', 'lai') message.pub('greet1', 'lai1') #订阅另一个话题,并发布 message.sub('greet1', hello) message.pub('greet1', 'lai1') #取消订阅 message.unsub('greet', hello) message.pub('greet', 'unsub') #声明一个话题 message.declare('greet2', 'lai2') #第一次订阅不用发布就可以收到预定义的消息
def _handler(self, sock, addr): channel = Channel(sock, Server._handle, self._spawn) message.pub(Server.NEW_CONNECTION, self, channel) channel._recv_let.join() message.pub(Server.LOST_CONNECTION, self, channel)
def insert_folder(mapper, connection, target): ''' FOLDER 插入数据库之前,进行数据验证 :param mapper: :param connection: :param target: FOLDER orm :return: ''' import dayu_database as db import util # 如果标记为"删除", 那么不验证 if target.active is False: return # 如果是root,那么不验证 if target.name == config.DAYU_DB_ROOT_FOLDER_NAME: return # 必须指定parent assert target.parent_id is not None # 如果是第一个层级(通常是project),那么必须指定 db_config_name 和 storage_config_name assert not (target.parent.name == config.DAYU_DB_ROOT_FOLDER_NAME and target.db_config_name is None) assert not (target.parent.name == config.DAYU_DB_ROOT_FOLDER_NAME and target.storage_config_name is None) assert not (target.parent.name == config.DAYU_DB_ROOT_FOLDER_NAME and target.pipeline_config_name is None) # 其他层级深度,沿用parent 的db_config_name if target.db_config_name is None: target.db_config_name = target.parent.db_config_name # 其他层级深度,沿用parent 的 storage_config_name if target.storage_config_name is None: target.storage_config_name = target.parent.storage_config_name # 其他层级深度,沿用parent 的 pipeline_config_name if target.pipeline_config_name is None: target.pipeline_config_name = target.parent.pipeline_config_name # 沿用parent 的type_name if target.type_name is None: target.type_name = target.parent.type_name # 沿用parent 的type_group_name if target.type_group_name is None: target.type_group_name = target.parent.type_group_name # 深度+1,这里会调用 db.mixin.DepthMixin 中的 @validate('depth') 函数 target.depth = target.parent.depth + 1 if target.depth == 2: target.top_id = target.parent_id target.top = target.parent else: target.top_id = target.parent.top_id target.top = target.parent.top # 通过读取对应的 db_config 内容,将用户输入的short name,替换成完整的name # 例如,对于shot 来说,用户输入name=0010,但是处理之后会得到 pl_0010 的完整name session = db.get_session() assert session is not None config_orm = util.get_db_config(target.db_config_name) parents = list(getattr(target, 'hierarchy', None)) depth_config = config_orm.config[str(target.depth)] selected_names = (x.name for index, x in enumerate(parents) if index in depth_config['to_name_param'][target.meaning]) target.name = depth_config['to_name'][target.meaning].format(*selected_names) # 确保没有重名orm try: assert next((x for x in target.parent.children if x.name == target.name and x.id != target.id), None) is None except: raise Exception(target.name) # 如果没有输入label,那么用name 赋值给label,方便GUI 读取 if target.label is None: target.label = target.name # 由于shotgun 的监听事件需要修改cloud_id 和cloud_table, 所以必须在这里强行发射信号 import message message.pub(('event', 'db', 'folder', 'commit', 'before'), mapper, connection, target)
def insert_folder(mapper, connection, target): # 由于shotgun 的监听事件需要修改cloud_id 和cloud_table, 所以必须在这里强行发射信号 import message message.pub(('event', 'db', 'user', 'commit', 'before'), mapper, connection, target)
def insert_file(mapper, connection, target): ''' 利用sqlalchemy 的监听机制,在FILE 写入数据库之前,进行数据校验 :param mapper: :param connection: :param target: :return: ''' import dayu_database as db import util # 如果删除,那么跳过检查 if target.active is False: return # 必须指定parent assert target.parent_id is not None # 继承parent 的db_config_name if target.db_config_name is None: target.db_config_name = target.parent.db_config_name # 继承parent 的 storage_config_name if target.storage_config_name is None: target.storage_config_name = target.parent.storage_config_name # 其他层级深度,沿用parent 的 pipeline_config_name if target.pipeline_config_name is None: target.pipeline_config_name = target.parent.pipeline_config_name # 继承parent 的type_name if target.type_name is None: target.type_name = target.parent.type_name # 继承parent 的type_group_name if target.type_group_name is None: target.type_group_name = target.parent.type_group_name # parent 的深度+1,会触发mixin.DepthMixin 的 @validate('depth') 函数 target.depth = target.parent.depth + 1 # 设置top 属性 if target.depth == 2: target.top_id = target.parent_id target.top = target.parent else: target.top_id = target.parent.top_id target.top = target.parent.top # 如果FILE 不指定文件名,那么很可能是meaning 为VERSION、DAILIES # 那么需要根据parent 文件夹内的已有文件,进行版本名自增 session = db.get_session() assert session is not None config_orm = util.get_db_config(target.db_config_name) if target.name is None: try: contents = target.parent.sub_files.filter(FILE.id != target.id)[-1] match = version_regex.match(str(contents.name)) if match: version_num = match.groups()[0] target.name = 'v%0{}d'.format(len(version_num)) % (int(version_num) + 1) # if target.old_file_id is None: # target.old_file_id = contents.id except: cas_info = db.util.get_cascading_info(target, 'cascading_info')['all_info'] target.name = cas_info.get('init_{}_version'.format(target.type_group_name), 'v0001') # 继续根据db_config 来进行完整name 的组合 parents = list(getattr(target, 'hierarchy', None)) depth_config = config_orm.config[str(target.depth)] selected_names = (x.name for index, x in enumerate(parents) if index in depth_config['to_name_param'][target.meaning]) target.name = depth_config['to_name'][target.meaning].format(*selected_names) # 保证没有重名 assert next((x for x in target.parent.children if x.name == target.name and x.id != target.id), None) is None # 如果没有label,那么把name 赋值给label,方便GUI 读取显示 if target.label is None: target.label = target.name # 由于shotgun 的监听事件需要修改cloud_id 和cloud_table, 所以必须在这里强行发射信号 import message message.pub(('event', 'db', 'file', 'commit', 'before'), mapper, connection, target)
def bar(): """重要函数,给外部调用者使用,执行前先打印些信息,方便调试查看""" message.pub(LOG_MSG, 'Haha, Calling bar().')
def __init__(self, name): print('Foo') self.name = name self.sub('greet', greet) def pub_greet(self): self.pub('greet', self) foo = Foo('lai') foo.pub_greet() # 除了简单的sub()/pub()之外,它还支持取消订阅(unsub())和中止消息传递 import message def hello(name): print('hello %s' % name) ctx = message.Context() ctx.discontinued = True return ctx def hi(name): print('you cann\'t see me') message.sub('greet', hello) message.sub('greet', hi) message.pub('greet', 'lai')
def publishSync(self, topic, content): message.pub(topic, content)