abseil / abseil-py

Abseil Common Libraries (Python)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can I use ABSL flags in a child process when multiprocessing?

haohang96 opened this issue · comments

FLAGS are destroyed in child process

Although there seems to be a solution : #107, it can not solve this problem. Because in child process, after FLAGS.read_flag_from_files(['--flagfile=said_fiel']) + FLAGS._parse_args(), the parsed flags are all classified into unknown_flags, or unparsed_args.

Do you have a simple re-producible example? Are your flags defined at module level? Alternatively, something like the following should work:

import multiprocessing
import sys
from absl import app
from absl import flags

FLAGS = flags.FLAGS
flags.DEFINE_string('flag', 'default', 'help')


def child(argv):
    print(f'argv = {argv}')
    print(f'FLAGS.flag = {FLAGS.flag}')


def main(argv):
    print(f'argv = {argv}')
    print(f'FLAGS.flag = {FLAGS.flag}')

    p = multiprocessing.Process(
        target=app.run, args=(child, ), kwargs={'argv': sys.argv})
    p.start()
    p.join()


if __name__ == '__main__':
    app.run(main)

Yes! By define flags at module level, my code works fine. Thanks very much! You help me a lot.