def METHOD_NAME(self, uid, gid=None):
    try:
        username = pwd.getpwuid(uid).pw_name
        if not gid:
            gid = pwd.getpwuid(uid).pw_gid
    except KeyError:
        username = None
        gid = uid
    if os.getuid() == uid:
        return
    if os.getuid() != 0:
        logging.warning('Running as a limited user, setuid() unavailable!')
        return
    logging.info(
        f'Worker {os.getpid()} is demoting to UID {uid} / GID {gid}...'
    )
    groups = [
        g.gr_gid
        for g in grp.getgrall()
        if username in g.gr_mem or g.gr_gid == gid
    ]
    os.setgroups(groups)
    os.setgid(gid)
    os.setuid(uid)
    logging.info(f'...done, new EUID {os.geteuid()} EGID {os.getegid()}')