Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'assume': 0.14; 'suggest': 0.14; '2.7': 0.14; 'thread': 0.14; '"in': 0.16; '"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; '\xc2\xa0if': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'variable': 0.18; 'wed,': 0.18; 'all,': 0.19; 'trying': 0.19; 'else,': 0.19; 'file,': 0.19; "hasn't": 0.19; '8bit%:5': 0.22; 'command': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'creating': 0.23; 'error': 0.23; 'script.': 0.24; 'specify': 0.24; 'visible': 0.24; 'versions': 0.24; 'cc:2**0': 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; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 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; 'url:python': 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; 'received:google.com': 0.35; 'there': 0.35; '14,': 0.36; 'skip:s 60': 0.36; 'done': 0.36; 'url:listinfo': 0.36; "i'll": 0.36; 'url:org': 0.36; 'should': 0.36; 'error.': 0.37; 'wrong': 0.37; 'two': 0.37; 'skip:& 10': 0.38; 'handle': 0.38; 'pm,': 0.38; 'that,': 0.38; 'skip:& 20': 0.39; 'skip:. 10': 0.39; 'sure': 0.39; 'unable': 0.39; 'url:mail': 0.40; 'how': 0.40; 'even': 0.60; 'around.': 0.60; 'dave': 0.60; 'skip:\xc2 10': 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; '8bit%:10': 0.64; 'telling': 0.64; 'provide': 0.64; 'more': 0.64; '2015': 0.84; 'bare': 0.84; 'popped': 0.84; 'subject:check': 0.84; 'angel': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=OPi1tVKcAyQzQz1jseOaw5VZI7IoWN1MoRA09AKHGN4=; b=gpvL1zECKX7hmFecjujbG5TdzKWrVsiFGoMMGSujKEO38sR639yvYJaH7ir0OR/fcJ PCLWIrZTfqSOyiRwgteyvBcvyLXqaa89xz4MgqdtnKmolAyDjwSc2tuQ+Lti5nck6rvy f/x8fI5O+9hLzJmhtS9mPBfXd8f4tbjJTS/w0ehBEFKImy6ZzvjwNCI1UPmPqAHSxY8u ZUW9LacNESISklRIJtkOKSmS7l8uJrFj0c019MHUbOnxGW344hMQFQoFaqw05gx92e4f GqjV87eC8M2kZueO/hTkb21WGl0T4PC42fvta4nkn4B1UENHUK8tHGiPI4/n9tn2yXsN OHyQ== MIME-Version: 1.0 X-Received: by 10.60.120.36 with SMTP id kz4mr2205686oeb.80.1421237495069; Wed, 14 Jan 2015 04:11:35 -0800 (PST) In-Reply-To: <54B6479B.6080607@davea.name> References: <54B6479B.6080607@davea.name> Date: Wed, 14 Jan 2015 17:41:35 +0530 Subject: Re: Threading in Python, Please check the script From: Robert Clove To: Dave Angel Content-Type: multipart/alternative; boundary=047d7b3393410903ee050c9ba402 Cc: "python-list@python.org" 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: 275 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1421237503 news.xs4all.nl 2869 [2001:888:2000:d::a6]:33021 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83746 --047d7b3393410903ee050c9ba402 Content-Type: text/plain; charset=UTF-8 Can u provide me the pseudo script. On Wed, Jan 14, 2015 at 4:10 PM, Dave Angel wrote: > 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 > -- > https://mail.python.org/mailman/listinfo/python-list > --047d7b3393410903ee050c9ba402 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Can u provide me the pseudo script.


On Wed, Jan 14, 2015 at 4:= 10 PM, Dave Angel <davea@davea.name> wrote:
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= 9;re using.=C2=A0 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<= br> 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 d= efined yet.=C2=A0 This top-level code should be moved to the end of the fil= e, after the if __name__ =3D "__main__": line

Or just drop it, since you've got a second set of code also trying to c= reate new threads.

command=3D "strace -o /root/Desktop/a.txt -c ./server"
final_dir =3D "/root/Desktop"
exitflag =3D 0
# Define a function for the thread
def print_time(*args):
=C2=A0 =C2=A0 =C2=A0os.chdir(final_dir)
=C2=A0 =C2=A0 =C2=A0print "IN first thread"
=C2=A0 =C2=A0 =C2=A0proc =3D subprocess.Popen(command,shell=3DTrue,s= tdout=3Dsubprocess.PIPE,
stderr=3Dsubprocess.PIPE)
=C2=A0 =C2=A0 =C2=A0proc.wait(70)
=C2=A0 =C2=A0 =C2=A0exitflag=3D1

You just set a local variable, not the global one.=C2=A0 So it won't be= visible in the other thread.=C2=A0 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):
=C2=A0 =C2=A0 =C2=A0print "In second thread"
=C2=A0 =C2=A0 =C2=A0global exitflag
=C2=A0 =C2=A0 =C2=A0while exitflag:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0thread.exit()
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0#proc =3D
subprocess.Popen(command1,shell=3DTrue,stdout=3Dsubprocess.PI= PE,
sterr=3Dsubprocess.PIPE)



# Create two threads as follows
try:
=C2=A0 =C2=A0 =C2=A0while (1):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0t1=3Dthreading.Thread(target=3Dpri= nt_time)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0t1.start()
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0t2=3Dthreading.Thread(target=3Dpri= nt_time1)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0t2=3Dstart()
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0time.sleep(80)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0z =3D t1.isAlive()
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0z1 =3D t2.isAlive()
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if z:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0z.exit()
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if z1:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0z1.exit()
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 threading.Thread(target=3Dprint_<= u>time1).start()
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 threading.Thread(target=3Dprint_<= u>time1).start()

What are you trying to do in those two lines?=C2=A0 If nothing else, they&#= 39;ll give an indentation error.=C2=A0 But if you fix that, you'll stil= l have the potential problem of creating more and more threads as you loop = around.

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0print "In try"
except:

Bare excepts are "evil."=C2=A0 Your user can't tell what went= wrong, from a syntax error to the user hitting control-C.=C2=A0 Even if yo= u can't handle a particular kind of exception, at least have the courte= sy of telling the user what went wrong.

=C2=A0 =C2=A0 print "Error: unable to start thread"




I'm sure there are other things, but these popped out at me.

--
DaveA
--
https://mail.python.org/mailman/listinfo/python-list

--047d7b3393410903ee050c9ba402--