How can I use ABSL flags in a child process when multiprocessing?
haohang96 opened this issue · comments
haohang96 commented
FLAGS are destroyed in child process
haohang96 commented
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.
Yilei Yang commented
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)
haohang96 commented
Yes! By define flags at module level, my code works fine. Thanks very much! You help me a lot.