code: def magic_argv_env_context(fn=None, timeout=300):
      """
      用来在测试时包裹每一个单独的测试函数，使得 ddp 测试正确；
      会丢掉 pytest 中的 arg 参数。
  
      :param timeout: 表示一个测试如果经过多久还没有通过的话就主动将其 kill 掉，默认为 5 分钟，单位为秒；
      :return:
      """
      # 说明是通过 @magic_argv_env_context(timeout=600) 调用；
      if fn is None:
          return partial(magic_argv_env_context, timeout=timeout)
  
      @wraps(fn)
      def wrapper(*args, **kwargs):
          command = deepcopy(sys.argv)
          env = deepcopy(os.environ.copy())
  
          used_args = []
          # for each_arg in sys.argv[1:]:
          #     # warning，否则 可能导致 pytest -s . 中的点混入其中，导致多卡启动的 collect tests items 不为 1
          #     if each_arg.startswith('-'):
          #         used_args.append(each_arg)
  
          pytest_current_test = os.environ.get("PYTEST_CURRENT_TEST")
  
          try:
              l_index = pytest_current_test.index("[")
              r_index = pytest_current_test.index("]")
              subtest = pytest_current_test[l_index : r_index + 1]
          except:
              subtest = ""
  
          if (
              not ismethod(fn)
              and get_class_that_defined_method(fn) is None
          ):
              sys.argv = [
                  sys.argv[0],
                  f"{os.path.abspath(sys.modules[fn.__module__].__file__)}::{fn.__name__}{subtest}",
              ] + used_args
          else:
              sys.argv = [
                  sys.argv[0],
                  f"{os.path.abspath(sys.modules[fn.__module__].__file__)}::{get_class_that_defined_method(fn).__name__}::{fn.__name__}{subtest}",
              ] + used_args
  
          def _handle_timeout(signum, frame):
              raise TimeoutError(
                  f"\nYour test fn: {fn.__name__} has timed out.\n"
              )
  
          # 恢复 logger
          handlers = [handler for handler in logger.handlers]
          formatters = [handler.formatter for handler in handlers]
          level = logger.level
  
          signal.signal(signal.SIGALRM, _handle_timeout)
          signal.alarm(timeout)
          res = fn(*args, **kwargs)
          signal.alarm(0)
          sys.argv = deepcopy(command)
          os.environ = env
  
          for formatter, handler in zip(formatters, handlers):
              handler.setFormatter(formatter)
          logger.handlers = handlers
          logger.setLevel(level)
  
          return res
  
      return wrapper
  
imports: import os
  import sys
  import __main__
  from functools import wraps, partial
  from inspect import ismethod
  from copy import deepcopy
  from io import StringIO
  import time
  import signal
  import pytest
  import numpy
  from fastNLP.core.utils.utils import get_class_that_defined_method
  from fastNLP.envs.env import FASTNLP_GLOBAL_RANK
  from fastNLP.core.drivers.utils import distributed_open_proc
  from fastNLP.core.log import logger
label_desc: Use image node_id 2 for calling a global function with example usage: magic_argv_env_context(fn, timeout) and returns: wrapper, partial, res