Why does UDP socket takes such long time in Python?











up vote
0
down vote

favorite














while i'm testing sockets in python ,i'm wondering why does it takes such long time to complete a UDP send & receive process.
(it takes about 2ms per contact LOCALLY with my code ,it's just too slow to use it communicating between processes or threads)



Am I doing some thing wrong ?(run in single thread or something?) Or it's because python is just that slow ?



Server demo code like this :



import socket,time

s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.bind('localhost',6000)

while True:
data, addr = s.recvfrom(1024)
s.sendto(b'',addr)


And client is like this:



import socket,time

s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

for i in range(100):
t = time.time()
s.sendto('localhost',6000)
s.recv(1024)
print(time.time() - t)


the output is like this:



0.00799250602722168
0.0020029544830322266
0.0010268688201904297
0.0010089874267578125
0.0010042190551757812
0.0020051002502441406
0.002005338668823242
0.002005338668823242
0.002005338668823242
0.002005338668823242
0.002005338668823242
0.002005338668823242
0.0020051002502441406
0.0020058155059814453
0.0027120113372802734
0.0010039806365966797
0.0020055770874023438
0.00200653076171875
0.002003908157348633
0.0010035037994384766
0.0020051002502441406
0.0020074844360351562
0.0010325908660888672
0.002005338668823242
0.002690553665161133
0.0010037422180175781
0.0020055770874023438
0.0020051002502441406
0.002005338668823242
0.002005338668823242
0.001806020736694336
0.0010192394256591797
0.0010271072387695312
0.0020055770874023438
0.002007007598876953
0.0020041465759277344
0.002004861831665039
0.001999378204345703
0.0020067691802978516
0.0019817352294921875
0.002007007598876953


Can anybody solve my problem?
thanks a lot










share|improve this question




























    up vote
    0
    down vote

    favorite














    while i'm testing sockets in python ,i'm wondering why does it takes such long time to complete a UDP send & receive process.
    (it takes about 2ms per contact LOCALLY with my code ,it's just too slow to use it communicating between processes or threads)



    Am I doing some thing wrong ?(run in single thread or something?) Or it's because python is just that slow ?



    Server demo code like this :



    import socket,time

    s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    s.bind('localhost',6000)

    while True:
    data, addr = s.recvfrom(1024)
    s.sendto(b'',addr)


    And client is like this:



    import socket,time

    s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

    for i in range(100):
    t = time.time()
    s.sendto('localhost',6000)
    s.recv(1024)
    print(time.time() - t)


    the output is like this:



    0.00799250602722168
    0.0020029544830322266
    0.0010268688201904297
    0.0010089874267578125
    0.0010042190551757812
    0.0020051002502441406
    0.002005338668823242
    0.002005338668823242
    0.002005338668823242
    0.002005338668823242
    0.002005338668823242
    0.002005338668823242
    0.0020051002502441406
    0.0020058155059814453
    0.0027120113372802734
    0.0010039806365966797
    0.0020055770874023438
    0.00200653076171875
    0.002003908157348633
    0.0010035037994384766
    0.0020051002502441406
    0.0020074844360351562
    0.0010325908660888672
    0.002005338668823242
    0.002690553665161133
    0.0010037422180175781
    0.0020055770874023438
    0.0020051002502441406
    0.002005338668823242
    0.002005338668823242
    0.001806020736694336
    0.0010192394256591797
    0.0010271072387695312
    0.0020055770874023438
    0.002007007598876953
    0.0020041465759277344
    0.002004861831665039
    0.001999378204345703
    0.0020067691802978516
    0.0019817352294921875
    0.002007007598876953


    Can anybody solve my problem?
    thanks a lot










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite













      while i'm testing sockets in python ,i'm wondering why does it takes such long time to complete a UDP send & receive process.
      (it takes about 2ms per contact LOCALLY with my code ,it's just too slow to use it communicating between processes or threads)



      Am I doing some thing wrong ?(run in single thread or something?) Or it's because python is just that slow ?



      Server demo code like this :



      import socket,time

      s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
      s.bind('localhost',6000)

      while True:
      data, addr = s.recvfrom(1024)
      s.sendto(b'',addr)


      And client is like this:



      import socket,time

      s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

      for i in range(100):
      t = time.time()
      s.sendto('localhost',6000)
      s.recv(1024)
      print(time.time() - t)


      the output is like this:



      0.00799250602722168
      0.0020029544830322266
      0.0010268688201904297
      0.0010089874267578125
      0.0010042190551757812
      0.0020051002502441406
      0.002005338668823242
      0.002005338668823242
      0.002005338668823242
      0.002005338668823242
      0.002005338668823242
      0.002005338668823242
      0.0020051002502441406
      0.0020058155059814453
      0.0027120113372802734
      0.0010039806365966797
      0.0020055770874023438
      0.00200653076171875
      0.002003908157348633
      0.0010035037994384766
      0.0020051002502441406
      0.0020074844360351562
      0.0010325908660888672
      0.002005338668823242
      0.002690553665161133
      0.0010037422180175781
      0.0020055770874023438
      0.0020051002502441406
      0.002005338668823242
      0.002005338668823242
      0.001806020736694336
      0.0010192394256591797
      0.0010271072387695312
      0.0020055770874023438
      0.002007007598876953
      0.0020041465759277344
      0.002004861831665039
      0.001999378204345703
      0.0020067691802978516
      0.0019817352294921875
      0.002007007598876953


      Can anybody solve my problem?
      thanks a lot










      share|improve this question

















      while i'm testing sockets in python ,i'm wondering why does it takes such long time to complete a UDP send & receive process.
      (it takes about 2ms per contact LOCALLY with my code ,it's just too slow to use it communicating between processes or threads)



      Am I doing some thing wrong ?(run in single thread or something?) Or it's because python is just that slow ?



      Server demo code like this :



      import socket,time

      s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
      s.bind('localhost',6000)

      while True:
      data, addr = s.recvfrom(1024)
      s.sendto(b'',addr)


      And client is like this:



      import socket,time

      s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

      for i in range(100):
      t = time.time()
      s.sendto('localhost',6000)
      s.recv(1024)
      print(time.time() - t)


      the output is like this:



      0.00799250602722168
      0.0020029544830322266
      0.0010268688201904297
      0.0010089874267578125
      0.0010042190551757812
      0.0020051002502441406
      0.002005338668823242
      0.002005338668823242
      0.002005338668823242
      0.002005338668823242
      0.002005338668823242
      0.002005338668823242
      0.0020051002502441406
      0.0020058155059814453
      0.0027120113372802734
      0.0010039806365966797
      0.0020055770874023438
      0.00200653076171875
      0.002003908157348633
      0.0010035037994384766
      0.0020051002502441406
      0.0020074844360351562
      0.0010325908660888672
      0.002005338668823242
      0.002690553665161133
      0.0010037422180175781
      0.0020055770874023438
      0.0020051002502441406
      0.002005338668823242
      0.002005338668823242
      0.001806020736694336
      0.0010192394256591797
      0.0010271072387695312
      0.0020055770874023438
      0.002007007598876953
      0.0020041465759277344
      0.002004861831665039
      0.001999378204345703
      0.0020067691802978516
      0.0019817352294921875
      0.002007007598876953


      Can anybody solve my problem?
      thanks a lot







      python performance sockets websocket udp






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 at 17:10









      VPfB

      4,11211128




      4,11211128










      asked Nov 22 at 17:04









      LeeRoermond

      31




      31
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          I get about two orders of magnitude faster than that (~0.01ms, 3.4GHz i7-6700) maybe localhost resolves somewhere strange and it needs to retry things?



          your code also doesn't work for me, am posting the code I used. server side first:



          import socket

          with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
          s.bind(('localhost',6000))
          while True:
          data, addr = s.recvfrom(1024)
          s.sendto(b'',addr)


          now client side:



          import socket
          from time import time

          addr = socket.getaddrinfo(
          'localhost', 6000,
          socket.AF_INET, socket.SOCK_DGRAM)[0]

          with socket.socket(*addr[:3]) as s:
          s.connect(addr[4])
          for i in range(1000):
          t1 = time()
          s.send(b'')
          t2 = time()
          s.recv(1024)
          t3 = time()
          if i % 100 == 0:
          print('{:.3f}ms {:.3f}ms'.format((t2 - t1) * 1000, (t3 - t2) * 1000))


          note that I use socket.connect() to try and cut down on resolution time, but this doesn't seem to make any difference for me. I get:



          0.218ms 0.006ms
          0.004ms 0.010ms
          0.004ms 0.007ms
          0.004ms 0.008ms
          0.004ms 0.011ms
          0.004ms 0.010ms
          0.004ms 0.010ms
          0.004ms 0.009ms
          0.004ms 0.010ms
          0.004ms 0.008ms


          back, i.e. first time around is slow, then it's quick



          unix domain sockets might be faster if you care less about portability, otherwise you could try zeromq which has nice Python packages






          share|improve this answer





















          • Thanks ! Perfectly solves my question ! After added socket.connect() transfers become rocketly fast (with each communication takes about 40μs on my computer) ,could you please explain why it gets way faster just after added this one single line magic code?
            – LeeRoermond
            Nov 26 at 12:14












          • Does it make any difference on reliability or something ?
            – LeeRoermond
            Nov 26 at 12:21










          • at a guess you were performing DNS lookups on every send(), this could perform a network round-trip and would take 1–2ms on a relatively fast network. doing the connect() once will cause resolution to happen once and then work with IP addresses from then onwards. you could test by putting getaddrinfo() into a loop and seeing how long it takes.
            – Sam Mason
            Nov 26 at 12:26










          • as far as reliability is concerned; it can affect things if you expect the IP address associated with a host name to change. obviously this won't affect localhost (which should always point to 127.0.0.1) but might affect other hostname, hence why python takes its time doing the DNS lookups for you
            – Sam Mason
            Nov 26 at 12:28










          • It turns out that changing 'localhost' into '127.0.0.1' makes difference,thanks!
            – LeeRoermond
            Nov 26 at 12:41











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435563%2fwhy-does-udp-socket-takes-such-long-time-in-python%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          I get about two orders of magnitude faster than that (~0.01ms, 3.4GHz i7-6700) maybe localhost resolves somewhere strange and it needs to retry things?



          your code also doesn't work for me, am posting the code I used. server side first:



          import socket

          with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
          s.bind(('localhost',6000))
          while True:
          data, addr = s.recvfrom(1024)
          s.sendto(b'',addr)


          now client side:



          import socket
          from time import time

          addr = socket.getaddrinfo(
          'localhost', 6000,
          socket.AF_INET, socket.SOCK_DGRAM)[0]

          with socket.socket(*addr[:3]) as s:
          s.connect(addr[4])
          for i in range(1000):
          t1 = time()
          s.send(b'')
          t2 = time()
          s.recv(1024)
          t3 = time()
          if i % 100 == 0:
          print('{:.3f}ms {:.3f}ms'.format((t2 - t1) * 1000, (t3 - t2) * 1000))


          note that I use socket.connect() to try and cut down on resolution time, but this doesn't seem to make any difference for me. I get:



          0.218ms 0.006ms
          0.004ms 0.010ms
          0.004ms 0.007ms
          0.004ms 0.008ms
          0.004ms 0.011ms
          0.004ms 0.010ms
          0.004ms 0.010ms
          0.004ms 0.009ms
          0.004ms 0.010ms
          0.004ms 0.008ms


          back, i.e. first time around is slow, then it's quick



          unix domain sockets might be faster if you care less about portability, otherwise you could try zeromq which has nice Python packages






          share|improve this answer





















          • Thanks ! Perfectly solves my question ! After added socket.connect() transfers become rocketly fast (with each communication takes about 40μs on my computer) ,could you please explain why it gets way faster just after added this one single line magic code?
            – LeeRoermond
            Nov 26 at 12:14












          • Does it make any difference on reliability or something ?
            – LeeRoermond
            Nov 26 at 12:21










          • at a guess you were performing DNS lookups on every send(), this could perform a network round-trip and would take 1–2ms on a relatively fast network. doing the connect() once will cause resolution to happen once and then work with IP addresses from then onwards. you could test by putting getaddrinfo() into a loop and seeing how long it takes.
            – Sam Mason
            Nov 26 at 12:26










          • as far as reliability is concerned; it can affect things if you expect the IP address associated with a host name to change. obviously this won't affect localhost (which should always point to 127.0.0.1) but might affect other hostname, hence why python takes its time doing the DNS lookups for you
            – Sam Mason
            Nov 26 at 12:28










          • It turns out that changing 'localhost' into '127.0.0.1' makes difference,thanks!
            – LeeRoermond
            Nov 26 at 12:41















          up vote
          0
          down vote



          accepted










          I get about two orders of magnitude faster than that (~0.01ms, 3.4GHz i7-6700) maybe localhost resolves somewhere strange and it needs to retry things?



          your code also doesn't work for me, am posting the code I used. server side first:



          import socket

          with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
          s.bind(('localhost',6000))
          while True:
          data, addr = s.recvfrom(1024)
          s.sendto(b'',addr)


          now client side:



          import socket
          from time import time

          addr = socket.getaddrinfo(
          'localhost', 6000,
          socket.AF_INET, socket.SOCK_DGRAM)[0]

          with socket.socket(*addr[:3]) as s:
          s.connect(addr[4])
          for i in range(1000):
          t1 = time()
          s.send(b'')
          t2 = time()
          s.recv(1024)
          t3 = time()
          if i % 100 == 0:
          print('{:.3f}ms {:.3f}ms'.format((t2 - t1) * 1000, (t3 - t2) * 1000))


          note that I use socket.connect() to try and cut down on resolution time, but this doesn't seem to make any difference for me. I get:



          0.218ms 0.006ms
          0.004ms 0.010ms
          0.004ms 0.007ms
          0.004ms 0.008ms
          0.004ms 0.011ms
          0.004ms 0.010ms
          0.004ms 0.010ms
          0.004ms 0.009ms
          0.004ms 0.010ms
          0.004ms 0.008ms


          back, i.e. first time around is slow, then it's quick



          unix domain sockets might be faster if you care less about portability, otherwise you could try zeromq which has nice Python packages






          share|improve this answer





















          • Thanks ! Perfectly solves my question ! After added socket.connect() transfers become rocketly fast (with each communication takes about 40μs on my computer) ,could you please explain why it gets way faster just after added this one single line magic code?
            – LeeRoermond
            Nov 26 at 12:14












          • Does it make any difference on reliability or something ?
            – LeeRoermond
            Nov 26 at 12:21










          • at a guess you were performing DNS lookups on every send(), this could perform a network round-trip and would take 1–2ms on a relatively fast network. doing the connect() once will cause resolution to happen once and then work with IP addresses from then onwards. you could test by putting getaddrinfo() into a loop and seeing how long it takes.
            – Sam Mason
            Nov 26 at 12:26










          • as far as reliability is concerned; it can affect things if you expect the IP address associated with a host name to change. obviously this won't affect localhost (which should always point to 127.0.0.1) but might affect other hostname, hence why python takes its time doing the DNS lookups for you
            – Sam Mason
            Nov 26 at 12:28










          • It turns out that changing 'localhost' into '127.0.0.1' makes difference,thanks!
            – LeeRoermond
            Nov 26 at 12:41













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          I get about two orders of magnitude faster than that (~0.01ms, 3.4GHz i7-6700) maybe localhost resolves somewhere strange and it needs to retry things?



          your code also doesn't work for me, am posting the code I used. server side first:



          import socket

          with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
          s.bind(('localhost',6000))
          while True:
          data, addr = s.recvfrom(1024)
          s.sendto(b'',addr)


          now client side:



          import socket
          from time import time

          addr = socket.getaddrinfo(
          'localhost', 6000,
          socket.AF_INET, socket.SOCK_DGRAM)[0]

          with socket.socket(*addr[:3]) as s:
          s.connect(addr[4])
          for i in range(1000):
          t1 = time()
          s.send(b'')
          t2 = time()
          s.recv(1024)
          t3 = time()
          if i % 100 == 0:
          print('{:.3f}ms {:.3f}ms'.format((t2 - t1) * 1000, (t3 - t2) * 1000))


          note that I use socket.connect() to try and cut down on resolution time, but this doesn't seem to make any difference for me. I get:



          0.218ms 0.006ms
          0.004ms 0.010ms
          0.004ms 0.007ms
          0.004ms 0.008ms
          0.004ms 0.011ms
          0.004ms 0.010ms
          0.004ms 0.010ms
          0.004ms 0.009ms
          0.004ms 0.010ms
          0.004ms 0.008ms


          back, i.e. first time around is slow, then it's quick



          unix domain sockets might be faster if you care less about portability, otherwise you could try zeromq which has nice Python packages






          share|improve this answer












          I get about two orders of magnitude faster than that (~0.01ms, 3.4GHz i7-6700) maybe localhost resolves somewhere strange and it needs to retry things?



          your code also doesn't work for me, am posting the code I used. server side first:



          import socket

          with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
          s.bind(('localhost',6000))
          while True:
          data, addr = s.recvfrom(1024)
          s.sendto(b'',addr)


          now client side:



          import socket
          from time import time

          addr = socket.getaddrinfo(
          'localhost', 6000,
          socket.AF_INET, socket.SOCK_DGRAM)[0]

          with socket.socket(*addr[:3]) as s:
          s.connect(addr[4])
          for i in range(1000):
          t1 = time()
          s.send(b'')
          t2 = time()
          s.recv(1024)
          t3 = time()
          if i % 100 == 0:
          print('{:.3f}ms {:.3f}ms'.format((t2 - t1) * 1000, (t3 - t2) * 1000))


          note that I use socket.connect() to try and cut down on resolution time, but this doesn't seem to make any difference for me. I get:



          0.218ms 0.006ms
          0.004ms 0.010ms
          0.004ms 0.007ms
          0.004ms 0.008ms
          0.004ms 0.011ms
          0.004ms 0.010ms
          0.004ms 0.010ms
          0.004ms 0.009ms
          0.004ms 0.010ms
          0.004ms 0.008ms


          back, i.e. first time around is slow, then it's quick



          unix domain sockets might be faster if you care less about portability, otherwise you could try zeromq which has nice Python packages







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 at 18:38









          Sam Mason

          2,39511226




          2,39511226












          • Thanks ! Perfectly solves my question ! After added socket.connect() transfers become rocketly fast (with each communication takes about 40μs on my computer) ,could you please explain why it gets way faster just after added this one single line magic code?
            – LeeRoermond
            Nov 26 at 12:14












          • Does it make any difference on reliability or something ?
            – LeeRoermond
            Nov 26 at 12:21










          • at a guess you were performing DNS lookups on every send(), this could perform a network round-trip and would take 1–2ms on a relatively fast network. doing the connect() once will cause resolution to happen once and then work with IP addresses from then onwards. you could test by putting getaddrinfo() into a loop and seeing how long it takes.
            – Sam Mason
            Nov 26 at 12:26










          • as far as reliability is concerned; it can affect things if you expect the IP address associated with a host name to change. obviously this won't affect localhost (which should always point to 127.0.0.1) but might affect other hostname, hence why python takes its time doing the DNS lookups for you
            – Sam Mason
            Nov 26 at 12:28










          • It turns out that changing 'localhost' into '127.0.0.1' makes difference,thanks!
            – LeeRoermond
            Nov 26 at 12:41


















          • Thanks ! Perfectly solves my question ! After added socket.connect() transfers become rocketly fast (with each communication takes about 40μs on my computer) ,could you please explain why it gets way faster just after added this one single line magic code?
            – LeeRoermond
            Nov 26 at 12:14












          • Does it make any difference on reliability or something ?
            – LeeRoermond
            Nov 26 at 12:21










          • at a guess you were performing DNS lookups on every send(), this could perform a network round-trip and would take 1–2ms on a relatively fast network. doing the connect() once will cause resolution to happen once and then work with IP addresses from then onwards. you could test by putting getaddrinfo() into a loop and seeing how long it takes.
            – Sam Mason
            Nov 26 at 12:26










          • as far as reliability is concerned; it can affect things if you expect the IP address associated with a host name to change. obviously this won't affect localhost (which should always point to 127.0.0.1) but might affect other hostname, hence why python takes its time doing the DNS lookups for you
            – Sam Mason
            Nov 26 at 12:28










          • It turns out that changing 'localhost' into '127.0.0.1' makes difference,thanks!
            – LeeRoermond
            Nov 26 at 12:41
















          Thanks ! Perfectly solves my question ! After added socket.connect() transfers become rocketly fast (with each communication takes about 40μs on my computer) ,could you please explain why it gets way faster just after added this one single line magic code?
          – LeeRoermond
          Nov 26 at 12:14






          Thanks ! Perfectly solves my question ! After added socket.connect() transfers become rocketly fast (with each communication takes about 40μs on my computer) ,could you please explain why it gets way faster just after added this one single line magic code?
          – LeeRoermond
          Nov 26 at 12:14














          Does it make any difference on reliability or something ?
          – LeeRoermond
          Nov 26 at 12:21




          Does it make any difference on reliability or something ?
          – LeeRoermond
          Nov 26 at 12:21












          at a guess you were performing DNS lookups on every send(), this could perform a network round-trip and would take 1–2ms on a relatively fast network. doing the connect() once will cause resolution to happen once and then work with IP addresses from then onwards. you could test by putting getaddrinfo() into a loop and seeing how long it takes.
          – Sam Mason
          Nov 26 at 12:26




          at a guess you were performing DNS lookups on every send(), this could perform a network round-trip and would take 1–2ms on a relatively fast network. doing the connect() once will cause resolution to happen once and then work with IP addresses from then onwards. you could test by putting getaddrinfo() into a loop and seeing how long it takes.
          – Sam Mason
          Nov 26 at 12:26












          as far as reliability is concerned; it can affect things if you expect the IP address associated with a host name to change. obviously this won't affect localhost (which should always point to 127.0.0.1) but might affect other hostname, hence why python takes its time doing the DNS lookups for you
          – Sam Mason
          Nov 26 at 12:28




          as far as reliability is concerned; it can affect things if you expect the IP address associated with a host name to change. obviously this won't affect localhost (which should always point to 127.0.0.1) but might affect other hostname, hence why python takes its time doing the DNS lookups for you
          – Sam Mason
          Nov 26 at 12:28












          It turns out that changing 'localhost' into '127.0.0.1' makes difference,thanks!
          – LeeRoermond
          Nov 26 at 12:41




          It turns out that changing 'localhost' into '127.0.0.1' makes difference,thanks!
          – LeeRoermond
          Nov 26 at 12:41


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435563%2fwhy-does-udp-socket-takes-such-long-time-in-python%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          What visual should I use to simply compare current year value vs last year in Power BI desktop

          How to ignore python UserWarning in pytest?

          Alexandru Averescu