Fix warehouse API timeout issue causing freezing
Browse files- Reduced warehouse API timeout from 15s to 5s
- Added threading with 2-3s timeout for warehouse calls
- Prevents chatbot from freezing on slow warehouse API
- Graceful fallback if warehouse API is unavailable
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <[email protected]>
app.py
CHANGED
@@ -35,7 +35,7 @@ def get_warehouse_stock(product_name):
|
|
35 |
try:
|
36 |
import re
|
37 |
warehouse_url = 'https://video.trek-turkey.com/bizimhesap-warehouse-xml.php'
|
38 |
-
response = requests.get(warehouse_url, verify=False, timeout=
|
39 |
|
40 |
if response.status_code != 200:
|
41 |
return None
|
@@ -497,14 +497,26 @@ def chatbot_fn(user_message, history, image=None):
|
|
497 |
# Extract product name from improved search result for warehouse stock
|
498 |
enhanced_response = product_result['response']
|
499 |
|
500 |
-
# Try to get warehouse stock info
|
501 |
try:
|
502 |
-
|
503 |
-
input_words = user_message.lower().split()
|
504 |
warehouse_info = ""
|
|
|
505 |
|
506 |
-
|
507 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
508 |
if warehouse_stock and warehouse_stock != ["Hiçbir mağazada stokta bulunmuyor"]:
|
509 |
warehouse_info = f"\n\n🏪 MAĞAZA STOK BİLGİLERİ:\n"
|
510 |
for store_info in warehouse_stock:
|
@@ -581,10 +593,28 @@ def chatbot_fn(user_message, history, image=None):
|
|
581 |
if len(product_info[1]) > 6 and product_info[1][6]: # Resim URL'si varsa
|
582 |
product_image = f"\\nÜrün resmi: {product_info[1][6]}"
|
583 |
|
584 |
-
# Mağaza stok bilgilerini ekle
|
585 |
warehouse_stock_info = ""
|
586 |
try:
|
587 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
588 |
if warehouse_stock and warehouse_stock != ["Hiçbir mağazada stokta bulunmuyor"]:
|
589 |
warehouse_stock_info = f"\\n🏪 Mağaza stok bilgileri:\\n"
|
590 |
for store_info in warehouse_stock:
|
|
|
35 |
try:
|
36 |
import re
|
37 |
warehouse_url = 'https://video.trek-turkey.com/bizimhesap-warehouse-xml.php'
|
38 |
+
response = requests.get(warehouse_url, verify=False, timeout=5)
|
39 |
|
40 |
if response.status_code != 200:
|
41 |
return None
|
|
|
497 |
# Extract product name from improved search result for warehouse stock
|
498 |
enhanced_response = product_result['response']
|
499 |
|
500 |
+
# Try to get warehouse stock info with quick timeout
|
501 |
try:
|
502 |
+
import threading
|
|
|
503 |
warehouse_info = ""
|
504 |
+
warehouse_result = [None]
|
505 |
|
506 |
+
def get_warehouse_improved():
|
507 |
+
try:
|
508 |
+
warehouse_result[0] = get_warehouse_stock(user_message)
|
509 |
+
except Exception as e:
|
510 |
+
print(f"Warehouse improved thread error: {e}")
|
511 |
+
warehouse_result[0] = None
|
512 |
+
|
513 |
+
# 2 saniye timeout ile warehouse call
|
514 |
+
warehouse_thread = threading.Thread(target=get_warehouse_improved)
|
515 |
+
warehouse_thread.daemon = True
|
516 |
+
warehouse_thread.start()
|
517 |
+
warehouse_thread.join(timeout=2)
|
518 |
+
|
519 |
+
warehouse_stock = warehouse_result[0]
|
520 |
if warehouse_stock and warehouse_stock != ["Hiçbir mağazada stokta bulunmuyor"]:
|
521 |
warehouse_info = f"\n\n🏪 MAĞAZA STOK BİLGİLERİ:\n"
|
522 |
for store_info in warehouse_stock:
|
|
|
593 |
if len(product_info[1]) > 6 and product_info[1][6]: # Resim URL'si varsa
|
594 |
product_image = f"\\nÜrün resmi: {product_info[1][6]}"
|
595 |
|
596 |
+
# Mağaza stok bilgilerini ekle (async için hızlı timeout)
|
597 |
warehouse_stock_info = ""
|
598 |
try:
|
599 |
+
import threading
|
600 |
+
import time
|
601 |
+
|
602 |
+
warehouse_result = [None]
|
603 |
+
|
604 |
+
def get_warehouse_quick():
|
605 |
+
try:
|
606 |
+
warehouse_result[0] = get_warehouse_stock(product_info[2])
|
607 |
+
except Exception as e:
|
608 |
+
print(f"Warehouse thread error: {e}")
|
609 |
+
warehouse_result[0] = None
|
610 |
+
|
611 |
+
# 3 saniye timeout ile warehouse call
|
612 |
+
warehouse_thread = threading.Thread(target=get_warehouse_quick)
|
613 |
+
warehouse_thread.daemon = True
|
614 |
+
warehouse_thread.start()
|
615 |
+
warehouse_thread.join(timeout=3)
|
616 |
+
|
617 |
+
warehouse_stock = warehouse_result[0]
|
618 |
if warehouse_stock and warehouse_stock != ["Hiçbir mağazada stokta bulunmuyor"]:
|
619 |
warehouse_stock_info = f"\\n🏪 Mağaza stok bilgileri:\\n"
|
620 |
for store_info in warehouse_stock:
|