Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3636690
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:03:54+00:00 2026-05-19T01:03:54+00:00

i’m not particularly new at python but i just thought there might be a

  • 0

i’m not particularly new at python but i just thought there might be a reason for this program not working properly. i have written a similar one from which this is derived, and that still works fine. it is a program to graph the average time of a set of ping responses, to see if there is any pattern in the time over the day. the source is as follows

from Tkinter import *
import matplotlib
import time
import os, sys, threading, Queue
matplotlib.use('TkAgg')
from numpy import arange, array, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure

import Tkinter
import sys
class App(object):
    def __init__(self):
        self.q = Queue.Queue()
        self.q2 = Queue.Queue()
        self.avvals=[]
        self.addlist=['bbc.co.uk', 'google.co.uk', 'nhgs.co.uk', 'bing.co.uk', 'msn.com']
        self.addlistlen = len(self.addlist)
        self.root = Tkinter.Tk()
        self.root.wm_title("Connection Speed")
        self.frame = Tkinter.Frame(self.root)
        self.frame.pack(side='top', expand=1, fill='both',)
        self.frame2 = Tkinter.Frame(self.frame)
        self.frame2.pack(side='bottom', expand=1, fill='both',)
        self.f = Figure(figsize=(5,4), dpi=100)
        self.a = self.f.add_subplot(111)
        self.gframe = Tkinter.Frame(self.frame)
        self.gframe.pack(side='top', expand=1, fill='both',)
        self.canvas = FigureCanvasTkAgg(self.f, master=self.gframe)
        self.canvas.show()
        self.canvas.get_tk_widget().pack(side='top', fill='both', expand=1)
        self.canvas._tkcanvas.pack(side='top', fill='both', expand=1)
        self.lt = threading.Thread(target=self.loop())
    def getping(self, add):
            pingaling = os.popen("ping -n 2 "+str(add).strip())
            sys.stdout.flush()
            line = pingaling.read()
            if line:
                try:
                    line = line.split('Maximum = ')[1]
                    time = line.split('ms, Average')[0]
                    self.q.put(int(time))
                except:
                    self.q.put(None)
    def gpthread(self, *a):
        t = threading.Thread(target=self.getping, args=a)
        t.isDaemon = True
        t.start()
    def loop(self):
        while 1:
            for x in self.addlist:
                self.gpthread(x)
            while self.q.qsize<self.addlistlen:
                pass
            tl = []
            for u in range(self.addlistlen):
                temp = self.q.get()
                if temp != None:
                    tl.append(temp)
            if len(tl)>0:
                self.update(sum(tl)/len(tl))
            else:
                self.update(None)
    def update(self, val):
        self.a.clear()
        self.avvals.append(val)
        self.a.plot(self.avvals, linewidth=0.5, color = 'b')
        self.canvas.draw()
a = App()
try:
    a.root.mainloop()
except:
    a.root.destroy()

i probably dont need the bottom try..except but i put it in to check if it would make a difference.
i haven’t had a chance to try it on another computer, but my other scripts are working fine so….
i simply can’t comprehend why it freezes, stops responding, and if i exit it by any method i get a error saying
Fatal python error: PyEval NULL tstate
or somthing very similar.
now it doesn’t even expand! it just goes straight to not responding!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T01:03:55+00:00Added an answer on May 19, 2026 at 1:03 am

    Change

    self.lt = threading.Thread(target=self.loop())
    

    to

    self.lt = threading.Thread(target=self.loop)
    

    target=self.loop() calls the loop method before passing the result to threading.Thread.

    Passing target=self.loop passes the method object to threading.Thread without calling it. This lets threading.Thread call the method in a new thread.


    Here is some code which uses threads to ping some ips, and displays the average ping times in an animated matplotlib bar chart, embedded in a Tkinter window:

    import Tkinter
    import threading
    import subprocess
    import Queue
    import shlex
    import re
    import matplotlib.pyplot as plt
    import matplotlib.backends.backend_tkagg as tkagg
    import atexit
    import numpy as np
    
    pingers=[]
    def cleanup():
        print('terminating ping subprocesses...')
        for pinger in pingers:
            pinger.proc.terminate()        
    atexit.register(cleanup)
    
    class Pinger(threading.Thread):
        def __init__(self,app,queue):
            threading.Thread.__init__(self)        
            self.app=app
            self.queue=queue
        def run(self):
            # One ping subprocess is started by each Pinger thread.
            # The ping subprocess runs indefinitely, terminated by the cleanup function
            # which is called by atexit right before the main program terminates.
            ip = self.queue.get()
            cmd="ping %s" % ip
            self.proc = subprocess.Popen(shlex.split(cmd),
                                         stdout=subprocess.PIPE)
            for line in iter(self.proc.stdout.readline,''):
                match=re.search('time=(.*)\s+ms',line)
                if match:
                    avg=float(match.group(1))
                    self.app.update(ip,avg)
    
    class App(object):
        def __init__(self,master,ips):
            self.ips=ips
            self.fig = plt.Figure(figsize=(5,4), dpi=100)
            self.fig.subplots_adjust(bottom=0.25) 
            self.ax=self.fig.add_subplot(1,1,1)
            self.canvas = tkagg.FigureCanvasTkAgg(self.fig, master=master)
            self.canvas.get_tk_widget().pack(side='top', fill='both', expand=1)
            self.canvas.show()
            N=len(self.ips)
            self.idx=dict(zip(self.ips,range(N)))
            # I set an initial ping time of 200 just to make the initial bar chart
            times=[200]*N  
            self.rects=self.ax.bar(range(N), times)
            self.ax.set_xticks(np.arange(N)+0.8*0.5)
            self.ax.set_xticklabels(self.ips, rotation=25)
        def update(self,ip,avg):
            # This is called by Pinger threads, each time a new ping value is obtained
            print(ip,avg)
            self.rects[self.idx[ip]].set_height(avg)
            self.canvas.draw()
    
    def main():    
        root = Tkinter.Tk()
        root.wm_title("Connection Speed")
        ips=['bbc.co.uk', 'google.co.uk', 'nhgs.co.uk', 'bing.co.uk', 'msn.com']
        app = App(root,ips)
        queue = Queue.Queue()
        for ip in ips:
            queue.put(ip)
            # This starts one Pinger for each ip.
            pinger=Pinger(app,queue)
            pingers.append(pinger)
            pinger.daemon=True
            pinger.start()
        Tkinter.mainloop()
    
    if __name__=='__main__':
        main()
    

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I want use html5's new tag to play a wav file (currently only supported
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I'm looking for suggestions for debugging... If you view this site in Firefox or
I need to clean up various Word 'smart' characters in user input, including but
I am currently running into a problem where an element is coming back from
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
In order to apply a triggered animation to all ToolTip s in my app,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.