find-blocking-calls.sh 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/bin/bash
  2. # Find potentially blocking calls in async Python code
  3. # Usage: ./find-blocking-calls.sh [directory]
  4. DIR="${1:-.}"
  5. echo "=== Scanning for blocking calls in async code ==="
  6. echo "Directory: $DIR"
  7. echo
  8. # time.sleep() in async functions
  9. echo "--- time.sleep() in async functions ---"
  10. rg -n "async def" -A 20 "$DIR" | rg "time\.sleep\(" || echo "None found"
  11. echo
  12. # requests library (blocking HTTP)
  13. echo "--- requests library usage ---"
  14. rg -n "import requests|from requests" "$DIR" --type py || echo "None found"
  15. echo
  16. # Blocking file operations
  17. echo "--- Blocking file operations in async ---"
  18. rg -n "async def" -A 30 "$DIR" | rg "open\(|\.read\(\)|\.write\(" || echo "None found"
  19. echo
  20. # subprocess without asyncio
  21. echo "--- Blocking subprocess calls ---"
  22. rg -n "subprocess\.(run|call|check_output)" "$DIR" --type py || echo "None found"
  23. echo
  24. # socket operations
  25. echo "--- Blocking socket operations ---"
  26. rg -n "socket\.(socket|create_connection)" "$DIR" --type py || echo "None found"
  27. echo
  28. # input() calls
  29. echo "--- Blocking input() calls ---"
  30. rg -n "\binput\(" "$DIR" --type py || echo "None found"
  31. echo
  32. echo "=== Recommendations ==="
  33. echo "- Replace time.sleep() with asyncio.sleep()"
  34. echo "- Replace requests with aiohttp or httpx"
  35. echo "- Replace open() with aiofiles"
  36. echo "- Replace subprocess with asyncio.create_subprocess_exec()"
  37. echo "- Use asyncio.get_event_loop().run_in_executor() for blocking code"