file_server.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # coding:utf-8
  2. from a2s.a2s_server import watch
  3. from a2s.a2s_client import a2s_execute
  4. import multiprocessing
  5. import random
  6. from concurrent.futures import ThreadPoolExecutor
  7. import time
  8. def start_test2img(data):
  9. # 本次不使用SSL,所以channel是不安全的
  10. try:
  11. for t in range(5):
  12. result = a2s_execute("192.168.3.240:9090", "test2img", 240, b"test")
  13. result = result.decode("utf-8")
  14. return result
  15. except Exception as e:
  16. print("test2img异常::",e)
  17. return ""
  18. @watch
  19. def run(data: bytes, *args, **kwargs):
  20. # 是否有图片
  21. a = random.randint(1, 2)
  22. print("picture:::>", a)
  23. if a == 1:
  24. # 有几张图片
  25. picture_num = random.randint(2, 40)
  26. pictures = [i for i in range(picture_num)]
  27. with ThreadPoolExecutor(max_workers=30) as executor:
  28. result = executor.map(start_test2img, pictures)
  29. list(result)
  30. a = random.randint(5, 20)
  31. time.sleep(a)
  32. return data
  33. def worker():
  34. run(nats_host="192.168.3.240", nats_port=19090, subject="test2txt", queue="main")
  35. def add_file_process():
  36. new_process = multiprocessing.Process(target=worker)
  37. new_process.start()
  38. return new_process