code: def test_many_futures():
      evt = mp.Event()
      receiver, sender = mp.Pipe()
      main_futures = [hivemind.MPFuture() for _ in range(1000)]
      assert len(hivemind.MPFuture._active_futures) == 1000
  
      def _run_peer():
          fork_futures = [hivemind.MPFuture() for _ in range(500)]
          assert len(hivemind.MPFuture._active_futures) == 500
  
          for i, future in enumerate(random.sample(main_futures, 300)):
              if random.random() < 0.5:
                  future.set_result(i)
              else:
                  future.set_exception(ValueError(f"{i}"))
  
          sender.send(fork_futures[:-100])
          for future in fork_futures[-100:]:
              future.cancel()
  
          evt.wait()
  
          assert len(hivemind.MPFuture._active_futures) == 200
          for future in fork_futures:
              if not future.done():
                  future.set_result(123)
          assert len(hivemind.MPFuture._active_futures) == 0
  
      p = mp.Process(target=_run_peer)
      p.start()
  
      some_fork_futures = receiver.recv()
  
      time.sleep(
          0.1
      )  # giving enough time for the futures to be destroyed
      assert len(hivemind.MPFuture._active_futures) == 700
  
      for future in some_fork_futures:
          future.set_running_or_notify_cancel()
      for future in random.sample(some_fork_futures, 200):
          future.set_result(321)
  
      evt.set()
      for future in main_futures:
          future.cancel()
      time.sleep(
          0.1
      )  # giving enough time for the futures to be destroyed
      assert len(hivemind.MPFuture._active_futures) == 0
      p.join()
  
imports: import asyncio
  import concurrent.futures
  import multiprocessing
  import random
  import time
  from concurrent.futures import ThreadPoolExecutor
  import numpy
  import pytest
  import torch
  import hivemind
  from hivemind.compression import deserialize_torch_tensor, serialize_torch_tensor
  from hivemind.proto.runtime_pb2 import CompressionType
  from hivemind.utils import BatchTensorDescriptor, DHTExpiration, HeapEntry, MSGPackSerializer, ValueWithExpiration
  from hivemind.utils.asyncio import achain, aenumerate, aiter_with_timeout, amap_in_executor, anext, as_aiter, asingle, attach_event_on_finished, azip, cancel_and_wait, enter_asynchronously
  from hivemind.utils.mpfuture import InvalidStateError
  from hivemind.utils.performance_ema import PerformanceEMA
label_desc: Use image node_id 7 for calling a global function with example usage: test_many_futures() without return types