code: def _make_invocation_counter(loop, stop_after=1):
      from types import MethodType
  
      counter = {"count": 0}
  
      def func():
          counter["count"] += 1
          if stop_after is not None and counter["count"] >= stop_after:
              loop.stop()
  
      def count(self):
          return self.counter["count"]
  
      func.count = MethodType(count, func)
      func.counter = counter
      return func
  
imports: from __future__ import annotations
  import pytest
  from concurrent.futures import ThreadPoolExecutor
  from itertools import repeat
  from tornado.ioloop import IOLoop
  from bokeh.util.serialization import make_id
  from bokeh.util.tornado import _CallbackGroup
label_desc: Use image node_id 1 for calling a global function with example usage: _make_invocation_counter(loop, stop_after) and returns: func, self