为什么gcloud命令启动缓慢?

时间:2016-10-05 03:20:44

标签: gcloud

只需输入1寻求帮助需要5秒钟。

gcloud

1 个答案:

答案 0 :(得分:7)

编辑2017-03-31:Zachary说gcloud 148.0.0解决了这个问题。所以试试gcloud components update。见https://stackoverflow.com/users/4922212/zachary-newman

TL;博士

事实证明,socket.gethostbyaddr(socket.gethostname())对于macOS中的.local主机名来说速度很慢。

$ python -i
>>> socket.gethostname()
'hiroshi-MacBook.local'
>>> socket.gethostbyaddr(socket.gethostname()) # it takes about 5 seconds
('localhost', ['1.0.0.127.in-addr.arpa'], ['127.0.0.1'])

因此,对于解决方法,只需将主机名添加到/ etc / hosts的localhost行。

127.0.0.1     localhost hiroshi-Macbook.local                             

之后返回值不同,但它会立即返回。

>>> socket.gethostbyaddr(socket.gethostname())
('localhost', ['1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa'], ['::1'])

我如何到达那里

gcloud命令在哪里:

$ which gcloud
/Users/hiroshi/google-cloud-sdk/bin/gcloud

编辑shell脚本的结束行语......

...
+ echo "$CLOUDSDK_PYTHON" $CLOUDSDK_PYTHON_ARGS "${CLOUDSDK_ROOT_DIR}/lib/gcloud.py" "$@"
"$CLOUDSDK_PYTHON" $CLOUDSDK_PYTHON_ARGS "${CLOUDSDK_ROOT_DIR}/lib/gcloud.py" "$@"

回显gcloud.py的位置:

$ gcloud
python2.7 -S /Users/hiroshi/google-cloud-sdk/lib/gcloud.py

行。谁花了5秒?

$ python2.7 -S -m cProfile -s time /Users/hiroshi/google-cloud-sdk/lib/gcloud.py
     173315 function calls (168167 primitive calls) in 5.451 seconds

Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    5.062    5.062    5.062    5.062 {_socket.gethostbyaddr}
...

_socket.gethostbyaddr是。

函数调用和backtrace的参数是什么样的? 我在gcloud.py

的main()之前添加了一些行
import traceback
def mygethostbyaddr(addr):
  print addr
  traceback.print_stack()
  return addr
import socket
socket.gethostbyaddr = mygethostbyaddr

再次执行gcloud。 我知道这是我机器的本地名称。

$ gcloud
hiroshi-MacBook.local
  File "/Users/hiroshi/google-cloud-sdk/lib/gcloud.py", line 74, in <module>
    main()
  File "/Users/hiroshi/google-cloud-sdk/lib/gcloud.py", line 70, in main
    sys.exit(googlecloudsdk.gcloud_main.main())
  File "/Users/hiroshi/google-cloud-sdk/lib/googlecloudsdk/gcloud_main.py", line 121, in main
    metrics.Started(START_TIME)
  File "/Users/hiroshi/google-cloud-sdk/lib/googlecloudsdk/core/metrics.py", line 411, in Wrapper
    return func(*args, **kwds)
  File "/Users/hiroshi/google-cloud-sdk/lib/googlecloudsdk/core/metrics.py", line 554, in Started
    collector = _MetricsCollector.GetCollector()
  File "/Users/hiroshi/google-cloud-sdk/lib/googlecloudsdk/core/metrics.py", line 139, in GetCollector
    _MetricsCollector._instance = _MetricsCollector()
  File "/Users/hiroshi/google-cloud-sdk/lib/googlecloudsdk/core/metrics.py", line 197, in __init__
    hostname = socket.getfqdn()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 141, in getfqdn
    hostname, aliases, ipaddrs = gethostbyaddr(name)
  File "/Users/hiroshi/google-cloud-sdk/lib/gcloud.py", line 32, in mygethostbyaddr
    traceback.print_stack()