def _runone(func, lockname, lockgroup, basedir, *args, **kwargs): ''' run one, AND ONLY ONE, instance (respect locking) >>> >>> _runone(print, 'lock', 'locks', '.', 'hello world') >>> ''' lock = MultiLock(lockname, lockgroup, basedir) if lock.acquire(): func(*args, **kwargs) lock.release()
s.shutdown(socket.SHUT_RD) except (socket.timeout, OSError): return None return time.time() - t async def send_to_changelog(guild: Guild, message: str): channel: Optional[TextChannel] = guild.get_channel(await run_in_thread(Settings.get, int, "logging_changelog", -1)) if channel is not None: await channel.send(message) event_handlers = {} cog_instances = {} handler_lock = MultiLock() async def call_event_handlers(event: str, *args, identifier=None, prepare=None): if identifier is not None: await handler_lock.acquire((event, identifier)) if prepare is not None: args = await prepare() if args is None: if identifier is not None: handler_lock.release((event, identifier)) return False for handler in event_handlers.get(event, []): if not await handler(*args):
'-g', dest="lockgroup", default='.locks', help="the lockgroup, a collection of locks independent locks") p.add_option( '--basedir', '-d', dest="basedir", default='.', help="the base directory where the lock files should be written") p.add_option( '--wait', '-w', dest="wait", default=None, help= "optional, wait (up till the number of seconds specified) for all locks to complete in the lockgroup" ) options, args = p.parse_args() if options.wait: lock = MultiLock(options.lockname, options.lockgroup, options.basedir) lock.wait(options.wait) sys.exit() @runone(options.lockname, options.lockgroup, options.basedir) def _main(): subprocess.call(args) _main()
def __init__(self, bot: Bot): self.bot = bot self.channel_lock = MultiLock() self.group_lock = MultiLock()
>>> >>> _runone(print, 'lock', 'locks', '.', 'hello world') >>> ''' lock = MultiLock(lockname, lockgroup, basedir) if lock.acquire(): func(*args, **kwargs) lock.release() if __name__ == '__main__': p = optparse.OptionParser('usage: %prog [options] cmd [args]') p.add_option('--lockname', '-l', dest="lockname", default='lock', help="the lock name, should be unique for this instance") p.add_option('--lockgroup', '-g', dest="lockgroup", default='.locks', help="the lockgroup, a collection of locks independent locks") p.add_option('--basedir', '-d', dest="basedir", default='.', help="the base directory where the lock files should be written") p.add_option('--wait', '-w', dest="wait", default=None, help="optional, wait (up till the number of seconds specified) for all locks to complete in the lockgroup") options, args = p.parse_args() if options.wait: lock = MultiLock(options.lockname, options.lockgroup, options.basedir) lock.wait(options.wait) sys.exit() @runone(options.lockname, options.lockgroup, options.basedir) def _main(): subprocess.call(args) _main()