Path: csiph.com!usenet.pasdenom.info!gegeweb.org!newsfeed0.kamp.net!newsfeed.kamp.net!87.79.20.101.MISMATCH!newsreader4.netcologne.de!news.netcologne.de!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'syntax': 0.04; 'yet.': 0.04; 'beginner': 0.05; 'subject:Python': 0.06; '"__main__":': 0.09; '__name__': 0.09; 'except:': 0.09; 'exception,': 0.09; 'function,': 0.09; 'skip:/ 10': 0.09; 'subject:script': 0.09; 'things,': 0.09; 'try:': 0.09; 'variable,': 0.09; 'wrong,': 0.09; 'python': 0.11; 'def': 0.12; 'assume': 0.14; 'suggest': 0.14; '2.7': 0.14; 'thread': 0.14; '"in': 0.16; '(none,': 0.16; 'declaration': 0.16; 'func': 0.16; 'proc': 0.16; 'referencing': 0.16; 'seconds.': 0.16; 'thread,': 0.16; 'threads.': 0.16; 'top-level': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'variable': 0.18; 'all,': 0.19; 'trying': 0.19; 'else,': 0.19; 'file,': 0.19; "hasn't": 0.19; 'command': 0.22; 'import': 0.22; 'print': 0.22; 'creating': 0.23; 'header:User-Agent:1': 0.23; 'error': 0.23; 'specify': 0.24; 'visible': 0.24; 'versions': 0.24; 'script': 0.25; 'define': 0.26; 'second': 0.26; 'least': 0.26; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'correct': 0.29; 'am,': 0.29; 'moved': 0.30; 'robert': 0.30; "i'm": 0.30; 'went': 0.31; 'code': 0.31; 'lines': 0.31; 'continues': 0.31; 'follows': 0.31; 'indentation': 0.31; "they'll": 0.31; 'run': 0.32; 'linux': 0.33; 'running': 0.33; 'skip:# 10': 0.33; 'skip:t 40': 0.33; 'subject:the': 0.34; 'message.': 0.35; 'problem': 0.35; "can't": 0.35; 'but': 0.35; 'there': 0.35; 'skip:s 60': 0.36; 'done': 0.36; "i'll": 0.36; 'should': 0.36; 'error.': 0.37; 'wrong': 0.37; 'two': 0.37; 'handle': 0.38; 'to:addr:python-list': 0.38; 'that,': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'unable': 0.39; 'how': 0.40; 'even': 0.60; 'around.': 0.60; 'tell': 0.60; 'break': 0.61; 'skip:t 30': 0.61; 'till': 0.61; 'new': 0.61; "you're": 0.61; 'first': 0.61; "you'll": 0.62; "you've": 0.63; 'kind': 0.63; 'telling': 0.64; 'more': 0.64; 'received:74.208': 0.68; 'bare': 0.84; 'popped': 0.84; 'subject:check': 0.84 Date: Wed, 14 Jan 2015 05:40:27 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Threading in Python, Please check the script References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:kakzW46Lvm+mJSO/tFMTJ4foKiAQtANG9gMGWenJVKc bQPb6pbXf5th+a4dTIcpAw8dCNGDs3ReCkzZvSKXff9E1sssaR xwQd82bR99IPtwdEeu7urb4dQALe0XcwAVE4Vo6pDZ6HhOlL+o 5BywggPUz5mS9AAdJt83MAsCymFuXynWMunhV12lF3GQ24HxK0 9hiX36nE608H8LO9vPk03xFyfo/srFxNeyCGbZzzH2ANeQEPOA i57iAKx+5dlx/UFIt7O5BeSsvna347OH5w7j4uCPgTWMAQji3d fV+cR/FzO5mIbYjHeqroZFSWEZm8rplKfvRjwCyUbk8ePC6BV9 t6/csNGvi6d/T3FQT4b8= X-UI-Out-Filterresults: notjunk:1; X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 106 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1421232033 news.xs4all.nl 2841 [2001:888:2000:d::a6]:42817 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83742 On 01/14/2015 01:22 AM, Robert Clove wrote: > Hi All, > In any new thread, you should specify what versions of Python and OS you're using. I'll assume Python 2.7 and Linux for this message. > I have made a script in which i have started two thread named thread 1 and > thread 2. > In thread 1 one function will run named func1 and in thread 2 function 2 > will run named func 2. > Thread 1 will execute a command and wait for 60 seconds. > Thread 2 will run only till thread 1 is running . > Again after that the same process continues in while after a break of 80 > Seconds. > > I am a beginner in python. > Please suggest what all i have done wrong and how to correct it. > > > #!/usr/bin/python > > import threading > import time > import subprocess > import datetime > import os > import thread > > thread.start_new_thread( print_time, (None, None)) > thread.start_new_thread( print_time1, (None, None)) In these two lines you're referencing a function that hasn't been defined yet. This top-level code should be moved to the end of the file, after the if __name__ = "__main__": line Or just drop it, since you've got a second set of code also trying to create new threads. > command= "strace -o /root/Desktop/a.txt -c ./server" > final_dir = "/root/Desktop" > exitflag = 0 > # Define a function for the thread > def print_time(*args): > os.chdir(final_dir) > print "IN first thread" > proc = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE, > stderr=subprocess.PIPE) > proc.wait(70) > exitflag=1 You just set a local variable, not the global one. So it won't be visible in the other thread. If you must rebind a top-level variable from a function, you need to use the 'global' declaration in your function. > > def print_time1(*args): > print "In second thread" > global exitflag > while exitflag: > thread.exit() > #proc = > subprocess.Popen(command1,shell=True,stdout=subprocess.PIPE, > sterr=subprocess.PIPE) > > > > # Create two threads as follows > try: > while (1): > t1=threading.Thread(target=print_time) > t1.start() > t2=threading.Thread(target=print_time1) > t2=start() > time.sleep(80) > z = t1.isAlive() > z1 = t2.isAlive() > if z: > z.exit() > if z1: > z1.exit() > threading.Thread(target=print_time1).start() > threading.Thread(target=print_time1).start() What are you trying to do in those two lines? If nothing else, they'll give an indentation error. But if you fix that, you'll still have the potential problem of creating more and more threads as you loop around. > print "In try" > except: Bare excepts are "evil." Your user can't tell what went wrong, from a syntax error to the user hitting control-C. Even if you can't handle a particular kind of exception, at least have the courtesy of telling the user what went wrong. > print "Error: unable to start thread" > > > I'm sure there are other things, but these popped out at me. -- DaveA