Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong Results when using Threading #26

Open
IslamMouhsen opened this issue Jul 19, 2020 · 3 comments
Open

Wrong Results when using Threading #26

IslamMouhsen opened this issue Jul 19, 2020 · 3 comments

Comments

@IslamMouhsen
Copy link

when i used threads to ping multiple ips in the same time
after the first request all other pings got None value
and i found that all values got the same id
image

@kyan001
Copy link
Owner

kyan001 commented Jul 19, 2020

Hi, can you run it in DEBUG mode? So I can figure out what is going on.

  1. import ping3; ping3.DEBUG = True
  2. run your codes.
  3. and then paste the outputs here.

and if you could show me your code that would be a great help.

@JimJamUrCode
Copy link

JimJamUrCode commented Aug 26, 2020

I am seeing a similar issue. I run this code on Ubuntu Linux 20.04 LTS and get IP addresses showing that I know are not on my network.

import threading
from queue import Queue

class ipInformation():
  def worker(self):
    while True:
      ip = self.q.get()
      self.pingIPTwo(ip)
      self.q.task_done()
      
  def scan(self):
    for x in range(1, 255):
      ip = "192.168.1." + str(x)
      self.q.put(ip)
    
    self.q.join()       # block until all tasks are done
    print("Done, workers/threads joined!!")

ipInfo = ipInformation()

for i in range(8):
  threading.Thread(target=ipInfo.worker, daemon=True).start()

ipInfo.scan()

@fohrloop
Copy link

fohrloop commented Jan 10, 2021

Hi,

Thank you for the work for this package. I have also noticed very strange behavior when using threads. I am on Windows 10.

Here is an example, where I use the ping3 package with and without multithreading to ping three different addresses:

  • 8.8.8.8 (Google's DNS)
  • 62.241.198..245 (DNS of my ISP)
  • google.com

The parts where threads were used is marked with orange boxes. When using just single thread, there is no coloring.

What can be seen from the figure?

  • The blue horizontal box shows the ping when there is no multithreading, when using ping3. There is clear difference in the average ping when using threading and when not using threading.
  • When using threading with ping3, the pings are always estimated too high.
  • When using threading with ping3, the pings have a lot of variation. All the ip addresses follow the same variation pattern.
  • When using the Windows ping command with subprocess.check_output, the the pings are behaving much better, and the ping level is the same with and without multithreading.

image

For reference, here is the simple piece of code used to make the pings behave reasonably with multithreading on Windows:

import re
import subprocess

def ping(addr, timeout=60):
    out = subprocess.check_output(
        ("ping", "-w", str(int(timeout) * 1000), addr)
    ).decode("utf-8")
    match = re.search(
        r"Minimum = (\d*).*?, Maximum = (\d*).*?, Average = (\d*).*?", out
    )
    if not match:
        return timeout
    return float(match.groups()[0]) / 1000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants