[問題] http傳輸速度

看板Python作者 (長常久九)時間7年前發表 (2017/10/02 15:07), 7年前編輯推噓3(309)
留言12則, 5人參與, 7年前最新討論串1/1
請問各位高手: 我在寫一個圖片辨認的程式(b程式),搭配既有的a程式 兩個程式是用http傳輸,程式流程為 1. a程式抓圖,傳圖片資料給b程式 2. b程式辨認圖片,傳辨認結果給a程式 在下面四種條件下,我測量a程式發出資料到接收到結果的時間,程式碼都沒改 當a程式 在Windows 7電腦,b程式 在Windows 7電腦,約為0.3秒 當a程式 在Windows 7電腦, b程式 在Ubuntu 16.03電腦,也是0.3秒 當a和b程式在同一台電腦,OS是Windows 7,約為1.3秒 當a和b程式在同一台電腦,OS是Ubuntu 16.03,約為0.2秒 請問為什麼a和b程式在同一台電腦,OS是Windows 7時,整體時間會特別慢? 我的程式碼如下 --------------a程式-------------- import numpy as np from PIL import Image import requests ... imgs0 = [Image.open(fileee) for fileee in imagefiles] imgs0 = np.array([np.array(resize(img0, r_shape)) for img0 in imgs0]) start = time.time() res = requests.post("http://localhost:8001", data=imgs0.tobytes()) #res = requests.post("http://192.168.1.150:8001", data=imgs0.tobytes()) ### client and server at different PC print("{:.3f}".format(time.time() - start)) ... --------------b程式-------------- import http.server import numpy as np ... class MyHandler(http.server.BaseHTTPRequestHandler): def do_POST(self): content_len = int(self.headers.get('content-length', 0)) post_body = self.rfile.read(content_len) imgs = np.frombuffer(post_body, dtype=np.uint8) imgs = imgs.reshape((-1,) + shape + (3,)) scores = model.predict(imgs)[:, 1].tobytes() self.send_response(200) self.end_headers() self.wfile.write(scores) server = http.server.HTTPServer(('127.0.0.1', 8001), MyHandler) #server = http.server.HTTPServer(('192.168.1.150', 8001), MyHandler) ### client and server at different PC server.serve_forever() -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 219.84.255.71 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1506956846.A.E46.html

10/03 00:11, 7年前 , 1F
把localhost換成127.0.0.1試試
10/03 00:11, 1F
謝謝ckclark大大,改了有效,時間也減少為0.3秒左右 我查一下傳輸變慢的原因,參考這篇文章 https://read01.com/zh-tw/nmB5Mz.html#.WdNG-VuCyUk 是因為我將server(b程式)設定為127.0.0.1,client(a程式)設為localhost 當client呼叫server時,需將locahost 轉為127.0.0.1,導致特別慢嗎? 為什麼在Ubuntu 下沒有這個問題?

10/03 00:50, 7年前 , 2F
借問一下 兩台電腦傳輸 是不是都要固定 IP?
10/03 00:50, 2F

10/03 00:51, 7年前 , 3F
我也想做兩台電腦傳輸 可是只有一台固定 IP 可以對外
10/03 00:51, 3F

10/03 00:57, 7年前 , 4F
利用 py 彼此傳輸的概念是 HTTPServer
10/03 00:57, 4F

10/03 00:58, 7年前 , 5F
架設臨時的伺服器嗎? 然後再用另一台去抓?
10/03 00:58, 5F

10/03 00:58, 7年前 , 6F
所以即使沒有固定 IP, 也沒關係?
10/03 00:58, 6F
我的方法如同lc85301大大所說 兩台電腦用網路跳線連接 一台當server,一台當client IP分別是192.168.1.x 和192.168.1.y Gateway 192.168.1.1 Mask 255.255.255.0 不過這兩台電腦都不對外,要連接外部網路要另外設定

10/03 07:26, 7年前 , 7F
對接 其中一台當 gateway 就好
10/03 07:26, 7F

10/03 07:27, 7年前 , 8F
沒有 IP 就只能用廣播的方式
10/03 07:27, 8F

10/03 11:14, 7年前 , 9F
是要做到像這樣?
10/03 11:14, 9F

10/03 11:14, 7年前 , 10F
※ 編輯: guestttttt (219.84.255.71), 10/03/2017 23:41:01

10/04 11:14, 7年前 , 11F
沒對外的話其實 gateway 也不用設
10/04 11:14, 11F

10/14 04:41, 7年前 , 12F
樓上是對的
10/14 04:41, 12F
文章代碼(AID): #1PqbOkv6 (Python)