fork download
  1. import requests
  2. import json
  3. import time
  4. from concurrent.futures import ThreadPoolExecutor, as_completed
  5.  
  6. # Konfigurasi
  7. API_URL = "https://a...content-available-to-author-only...1.com/index/user/do_register"
  8. BATCH_SIZE = 1000
  9. TOTAL_DATA = 5
  10. MAX_WORKERS = 10 # Jumlah thread paralel
  11.  
  12. # Fungsi untuk generate dummy data
  13. def generate_data(index):
  14. return {
  15. "id": index,
  16. "name": f"Item {index}",
  17. "value": index * 2,
  18. }
  19.  
  20. # Fungsi untuk mengirim 1 batch data
  21. def send_batch(batch_data):
  22. try:
  23. response = requests.post(API_URL, json=batch_data, timeout=10)
  24. response.raise_for_status()
  25. return {"success": True, "count": len(batch_data)}
  26. except requests.RequestException as e:
  27. print(f"Error sending batch: {e}")
  28. return {"success": False, "error": str(e)}
  29.  
  30. # Fungsi utama untuk proses batching dan feeding
  31. def main():
  32. start_time = time.time()
  33. with ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
  34. futures = []
  35. for i in range(0, TOTAL_DATA, BATCH_SIZE):
  36. batch = [generate_data(j) for j in range(i, min(i + BATCH_SIZE, TOTAL_DATA))]
  37. futures.append(executor.submit(send_batch, batch))
  38.  
  39. total_sent = 0
  40. for future in as_completed(futures):
  41. result = future.result()
  42. if result.get("success"):
  43. total_sent += result["count"]
  44.  
  45. end_time = time.time()
  46. print(f"Total sent: {total_sent} data")
  47. print(f"Elapsed time: {end_time - start_time:.2f} seconds")
  48.  
  49. if __name__ == "__main__":
  50. main()
  51.  
Success #stdin #stdout 1.27s 36476KB
stdin
Standard input is empty
stdout
Error sending batch: HTTPSConnectionPool(host='api.intudoventures011.com', port=443): Max retries exceeded with url: /index/user/do_register (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x14c21831bf20>: Failed to resolve 'api.intudoventures011.com' ([Errno -3] Temporary failure in name resolution)"))
Total sent: 0 data
Elapsed time: 0.06 seconds