这是一个奇怪的现象。
The code works normally on Windows (PC), but an error message occurs in Google Cloud as well as AWS, Azure
给定以下代码:
import ccxt
import pandas as pd
from pandas import DataFrame
import numpy as np
import multiprocessing as mp
# binance keys
API_KEY = ''
SECRET_KEY = ''
# binance API
exchange = ccxt.binance()
exchange.apiKey = API_KEY
exchange.secret = SECRET_KEY
exchange.options = {'defaultType': 'future', 'adjustForTimeDifference': True, 'recvWindow' : 10000,'defaultTimeInForce': 'GTC', 'timeDifference': 0}
coin_lists = ['REN/USDT', 'ZIL/USDT', 'CELR/USDT', 'TRX/USDT', 'VET/USDT', 'RVN/USDT','ARPA/USDT','ANKR/USDT','IOTX/USDT','XEM/USDT', '1000XEC/USDT']
# ticker's price
def cur_price(symbol):
ohlc = exchange.fetchOHLCV(symbol=symbol, timeframe='5m', limit=1)
return ohlc[0][4]
if __name__ == '__main__':
# test code
balance = exchange.fetch_balance()
df_1 = DataFrame(balance)
# main code
pool = mp.Pool(processes =None)
ticker_price = list(pool.map(cur_price, coin_lists))
print(ticker_price)
pool.close()
pool.join()
我的环境: 桌面: window10,vscode
谷歌云: Ubuntu 21.10
Python: 3.8.12 64 位
现象:下面的可执行代码在Windows(PC)上正常运行。但是,在使用 ubuntu 的 Google Cloud Platform 等云服务中会出现错误。
The code in question(Part of doubt):
如果注释掉#test 代码部分,它在Windows 和Goodle Cloud 上正常运行。仅当 'balance = exchange.fetch_balance()' 与下面的#main 代码部分一起时才会发生错误。但在 Windows 上它仍然可以正常工作。
此代码对于检查余额是绝对必要的。
Please help me!!!
错误信息
"""
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 699, in urlopen
httplib_response = self._make_request(
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 445, in _make_request
six.raise_from(e, None)
File "<string>", line 3, in raise_from
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 440, in _make_request
httplib_response = conn.getresponse()
File "/usr/lib/python3.9/http/client.py", line 1371, in getresponse
response.begin()
File "/usr/lib/python3.9/http/client.py", line 319, in begin
version, status, reason = self._read_status()
File "/usr/lib/python3.9/http/client.py", line 288, in _read_status
raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 439, in send
resp = conn.urlopen(
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 755, in urlopen
retries = retries.increment(
File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 532, in increment
raise six.reraise(type(error), error, _stacktrace)
File "/usr/lib/python3/dist-packages/six.py", line 718, in reraise
Traceback (most recent call last):
File "/usr/lib/python3.9/multiprocessing/pool.py", line 125, in worker
result = (True, func(*args, **kwds))
exchange.apiKey = API_KEY
File "/usr/lib/python3.9/multiprocessing/pool.py", line 48, in mapstar
return list(map(*args))
File "/home/g0xdroneart/auto_trading/t.py", line 26, in cur_price
ohlc = exchange.fetchOHLCV(symbol=symbol, timeframe='5m', limit=1)
File "/home/g0xdroneart/.local/lib/python3.9/site-packages/ccxt/binance.py", line 1887, in fetch_ohlcv
response = getattr(self, method)(self.extend(request, params))
File "/home/g0xdroneart/.local/lib/python3.9/site-packages/ccxt/base/exchange.py", line 457, in inner
return entry(_self, **inner_kwargs)
File "/home/g0xdroneart/.local/lib/python3.9/site-packages/ccxt/binance.py", line 4401, in request
response = self.fetch2(path, api, method, params, headers, body, config, context)
File "/home/g0xdroneart/.local/lib/python3.9/site-packages/ccxt/base/exchange.py", line 502, in fetch2
return self.fetch(request['url'], request['method'], request['headers'], request['body'])
File "/home/g0xdroneart/.local/lib/python3.9/site-packages/ccxt/base/exchange.py", line 639, in fetch
raise NetworkError(details) from e
ccxt.base.errors.NetworkError: binance GET https://fapi.binance.com/fapi/v1/klines?interval=5m&limit=1&sym
bol=CELRUSDT
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/g0xdroneart/auto_trading/t.py", line 37, in <module>
ticker_price = list(pool.map(cur_price, coin_lists))
File "/usr/lib/python3.9/multiprocessing/pool.py", line 364, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "/usr/lib/python3.9/multiprocessing/pool.py", line 771, in get
raise self._value
ccxt.base.errors.NetworkError: binance GET https://fapi.binance.com/fapi/v1/klines?interval=5m&limit=1&sym
bol=CELRUSDT