code: def batch_and_send_frames(args):
      # Initialize variables
      count, exit_cnt, snd_cnt, log_cnt = 0, 0, 0, 20
      payload, futures = {}, []
      start_time = time.time()
      fps = 0
      session = FuturesSession()
  
      while True:
          # Exit condition for the while loop. Need a better logic
          if len(queue) == 0:
              exit_cnt += 1
              # By trial and error, 1000 seemed to work best
              if exit_cnt >= 1000:
                  print(
                      "Length of queue is {} , snd_cnt is {}".format(
                          len(queue), snd_cnt
                      )
                  )
                  break
  
          if args.client_batching:
              # Batch the frames into a dict payload
              while queue and count < args.batch_size:
                  data = queue.popleft()
                  im_b64 = base64.b64encode(data).decode("utf8")
                  payload[str(count)] = im_b64
                  count += 1
  
              if count >= args.batch_size:
                  response, snd_cnt = send_frames(
                      payload, snd_cnt, session
                  )
  
                  if snd_cnt % log_cnt == 0:
                      # Calculate FPS
                      fps = calculate_fps(start_time, snd_cnt)
  
                      # Printing the response
                      print(response.content.decode("UTF-8"))
  
                  # Reset for next batch
                  start_time = time.time()
                  payload = {}
                  count = 0
          else:
              # If queue is not empty, send one frame at a time
              if queue:
                  payload = queue.popleft()
  
                  response, snd_cnt = send_frames(
                      payload, snd_cnt, session
                  )
                  futures.append(response)
  
                  if snd_cnt % log_cnt == 0:
                      # Calculate FPS
                      fps = calculate_fps(start_time, snd_cnt)
  
                      # Printing the response
                      for response in list(as_completed(futures))[-4:]:
                          print(
                              response.result().content.decode("utf-8")
                          )
  
                      # Cleaning up futures in case futures becomes too large
                      del futures[:log_cnt]
  
                  # Reset for next batch
                  start_time = time.time()
                  payload = None
  
          # Sleep for 10 ms before trying to send next batch of frames
          time.sleep(args.sleep)
  
      # Send any remaining frames
      _, snd_cnt = send_frames(payload, snd_cnt, session)
      print(
          "With Batch Size {}, FPS at frame number {} is {:.1f}".format(
              args.batch_size, snd_cnt, fps
          )
      )
  
file_path: serve/examples/image_classifier/near_real_time_video/request.py
imports: import argparse
  import base64
  import json
  import time
  from collections import deque
  from concurrent.futures import as_completed
  from threading import Thread
  import cv2
  import requests
  from requests_futures.sessions import FuturesSession
label_desc: Use image node_id 4 for calling a global function with example usage: batch_and_send_frames(args) without return types