测试代码改为以test_开头
This commit is contained in:
73
test_celery_api_list.py
Normal file
73
test_celery_api_list.py
Normal file
@@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env python3
|
||||
"""使用Celery任务遍历API配置列表执行调用"""
|
||||
|
||||
from invoke_call_api_task import invoke_call_api_task
|
||||
from api_config import API_CONFIG
|
||||
|
||||
def test_celery_api_list():
|
||||
"""使用Celery异步任务遍历所有API配置"""
|
||||
print("🚀 使用Celery任务遍历API配置列表")
|
||||
print("=" * 50)
|
||||
|
||||
results = []
|
||||
task_ids = []
|
||||
|
||||
# 提交所有任务
|
||||
for api_key in API_CONFIG.keys():
|
||||
print(f"📤 提交任务: {api_key}")
|
||||
|
||||
# 提交异步任务
|
||||
task_result = invoke_call_api_task.delay(api_key=api_key)
|
||||
task_ids.append((api_key, task_result))
|
||||
|
||||
results.append({
|
||||
'api_key': api_key,
|
||||
'task_id': task_result.id,
|
||||
'status': 'submitted'
|
||||
})
|
||||
|
||||
print(f"\n⏳ 等待 {len(task_ids)} 个任务完成...")
|
||||
|
||||
# 等待所有任务完成
|
||||
for api_key, task_result in task_ids:
|
||||
try:
|
||||
# 等待任务结果
|
||||
result = task_result.get(timeout=60)
|
||||
|
||||
# 更新结果
|
||||
for r in results:
|
||||
if r['api_key'] == api_key:
|
||||
r['status'] = result['status']
|
||||
r['message'] = result['message']
|
||||
r['task_result'] = result
|
||||
break
|
||||
|
||||
status_icon = "✅" if result['status'] == 'success' else "❌"
|
||||
print(f" {status_icon} {api_key}: {result['status']}")
|
||||
|
||||
except Exception as e:
|
||||
for r in results:
|
||||
if r['api_key'] == api_key:
|
||||
r['status'] = 'error'
|
||||
r['message'] = str(e)
|
||||
break
|
||||
print(f" ❌ {api_key}: 错误 - {str(e)[:30]}")
|
||||
|
||||
# 汇总
|
||||
print("\n" + "=" * 50)
|
||||
print("📊 Celery任务执行汇总:")
|
||||
|
||||
success_count = len([r for r in results if r['status'] == 'success'])
|
||||
total_count = len(results)
|
||||
|
||||
for result in results:
|
||||
status_icon = "✅" if result['status'] == 'success' else "❌"
|
||||
print(f" {status_icon} {result['api_key']}: {result['status']}")
|
||||
|
||||
print(f"\n🎯 总计: {success_count}/{total_count} 成功")
|
||||
print("✨ 所有任务执行完成")
|
||||
|
||||
return results
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_celery_api_list()
|
||||
Reference in New Issue
Block a user