Python Thread Execution Issue . . .


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Python Thread Execution Issue . . .
# 1  
Old 07-10-2015
Python Thread Execution Issue . . .

Greetings!

I set up a basic threading specimen which does the job:
Code:
#!/usr/bin/python

import threading


class a(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        print("thread a finished")


class b(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        threading.Timer(5.0, self.tDone).start()
        
    def tDone(self):
        print("thread b finished")


if __name__ == '__main__':

    thread1 = a()
    thread2 = b()
    
    thread1.start()
    thread2.start()
    
    thread1.join()
    thread2.join()

Simple, but effective Smilie

Now, taking this basic construct and adding GTK bookends into the mix for guibuilding,
Code:
#!/usr/bin/python

import gtk
import threading


class a(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        print("thread a finished")


class b(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        threading.Timer(5.0, self.tDone).start()
        
    def tDone(self):
        print("thread b finished")


if __name__ == '__main__':

    thread1 = a()
    thread2 = b()
    
    thread1.start()
    thread2.start()
    
    thread1.join()
    thread2.join()
    
gtk.main()

...the otherwise healthy code hangs in class b at threading.Timer with the following effects:
  • Timer runs through its wait period (5 sec.); and halts short of calling tDone.
  • If CTRL + C is invoked before Timer has completed its wait, the residual timing will be executed normally; and tDone will be called at the correct interval.
Here's what this looks like from the terminal:
Code:
thread a finished
^CTraceback (most recent call last):
  File "foo", line 37, in <module>
    gtk.main()
KeyboardInterrupt
thread b finished

So, I guess the question is: How can GTK be incorporated into the preceding example without causing a thread hangup involving Timer?


Thanks again --

---------- Post updated at 01:22 PM ---------- Previous update was at 11:20 AM ----------

Fixed. It was a GTK exit issue to unravel:
Code:
#!/usr/bin/python

import gtk
import gobject
import threading


class a(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        print("thread a finished")
        gobject.timeout_add_seconds(4, gtk.main_quit)


class b(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        threading.Timer(5.0, self.tDone).start()
        
    def tDone(self):
        print("thread b finished")


if __name__ == '__main__':

    thread1 = a()
    thread2 = b()
    
    thread1.start()
    thread2.start()
    
    thread1.join()
    thread2.join()

gtk.main()

All OK --

Cheers!

Last edited by LinQ; 07-10-2015 at 01:42 PM.. Reason: typo ;)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

[Python] - subprocess issue

So I have this basic script, see below import subprocess import shlex command = "gcloud projects list" subprocess.check_output(shlex.split(command)) subprocess.check_call(shlex.split(command)) The subprocess.check_call(shlex.split(command)) actually return what I expect. It returns... (6 Replies)
Discussion started by: scj2012
6 Replies

2. Programming

Help needed to Spawn Shell on Python and Continue Execution

def gob(url): print "\n\t Running gobuster on target." params = " -e -s '307,200,204,301,302' -t 20 -u " + url + " >> /tmp/%s/gobuster.txt" % (ip) os.system("xterm -e bash -c "tail -f /tmp/%/gobuster.txt"") for i in bflist: dirbf = "gobuster -w " + i... (3 Replies)
Discussion started by: alvinoo
3 Replies

3. Shell Programming and Scripting

Issue Spliting String in Python

I have a string like below Note: I have have a single to any number of comma "," seperated string assigned to jdbc_trgt variable. I need to split jdbc_trgt using comma(,) as the delimiter. I tried the below but it fails as i dont know how can i read each split string iterately. for... (4 Replies)
Discussion started by: mohtashims
4 Replies

4. Shell Programming and Scripting

Multiple Interval cron issue with order of execution

55,0 5,6 * * * myScript This cron task will execute on 5:00AM, 5:55AM, 6:00AM and 6:55AM. Is there any possibility to make it run only in the order specified such as 5:55AM and 6:00AM (basically only on the 2 intervals) ? (1 Reply)
Discussion started by: vikram3.r
1 Replies

5. Shell Programming and Scripting

Measure thread execution (in C, unix)

Hi, I have a simulation program which creates two threads and I would like to know if I can measure the time of each individual thread. Threads communicate (I use pthread to manage them) and I want to measure communication time. I found a solution with clock_gettime and CLOCK_THREAD_CPUTIME_ID... (32 Replies)
Discussion started by: Tinkh
32 Replies

6. Programming

Python Execution Stack

I was wondering if Python's execution stack does not grow if we make recursive call of a function i.e. if we implement a fibonacci () and factorial () functions and made recursive call of them in a conditional statement the execution stack will not grow? I wish someone answer this question with... (1 Reply)
Discussion started by: pharaoh
1 Replies

7. Shell Programming and Scripting

Crontab Permissions Issue with Python

I have a cron on a Linux server that isn't executing properly. CRON (with specific info replaced): MAILTO=emailaddress@server.com */2 * * * * python /data/site/cron.py OUTPUT: python: can't open file '/data/site/cron.py ': No such file or directoryAdditional info - The python path is... (3 Replies)
Discussion started by: theHire
3 Replies

8. Shell Programming and Scripting

Expect Issue Serial Forground Execution vs Concurrent Background Execution

I have an expect script that interrogates several hundred unix servers for both access and directories therein using "ssh user@host ls -l /path". The combination of host/path are unique but the host may be interrogated multiple times if there are multiple paths to test. The expect script is run... (2 Replies)
Discussion started by: twk
2 Replies

9. Shell Programming and Scripting

Execution issue with shell script - works in a different environment

Hi I get the following error while executing the shell script. I did not get an error when I ran the script in a different environment (unix server). str-token.ksh: 0403-057 Syntax error at line 20 : `(' is not expected. This is the line which gives error string=(${pos_array}) Please find... (3 Replies)
Discussion started by: hidnana
3 Replies

10. UNIX for Dummies Questions & Answers

Profile execution issue

Hi I have a profile execution issue, I log on to a linux machine , then i do sudo to another user as sudo su - <username> , then <username> .profile executes properly but when I type something I loose all environment varaible and my prompt changes to '$' loosing the PS1 value that I have... (1 Reply)
Discussion started by: malavm
1 Replies
Login or Register to Ask a Question