Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2a.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'beginner': 0.05; 'subject:Python': 0.06; 'except:': 0.09; 'skip:/ 10': 0.09; 'subject:script': 0.09; 'try:': 0.09; 'def': 0.12; 'suggest': 0.14; 'thread': 0.14; '"in': 0.16; '"in': 0.16; '(none,': 0.16; 'func': 0.16; 'proc': 0.16; 'seconds.': 0.16; '\xc2\xa0if': 0.16; 'all,': 0.19; '8bit%:5': 0.22; 'command': 0.22; 'import': 0.22; 'to:name:python-list@python.org': 0.22; 'print': 0.22; 'script': 0.25; 'define': 0.26; 'second': 0.26; 'function': 0.29; 'correct': 0.29; 'message-id:@mail.gmail.com': 0.30; 'continues': 0.31; 'follows': 0.31; 'run': 0.32; 'running': 0.33; 'skip:# 10': 0.33; 'skip:t 40': 0.33; 'subject:the': 0.34; 'received:google.com': 0.35; 'skip:s 60': 0.36; 'done': 0.36; 'wrong': 0.37; 'two': 0.37; '8bit%:4': 0.38; 'to:addr:python- list': 0.38; 'skip:& 20': 0.39; 'skip:. 10': 0.39; 'to:addr:python.org': 0.39; 'unable': 0.39; 'how': 0.40; 'skip:\xc2 10': 0.60; 'break': 0.61; 'skip:t 30': 0.61; 'till': 0.61; 'first': 0.61; '8bit%:100': 0.72; '\xc2\xa0\xc2\xa0': 0.74; 'subject:check': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=MPWO+P91sMh8Y/yvZa2lWyHkXtkKr+MjWP/i1d3gVuc=; b=mKl703K5AXUYsjD1eIyFmL+NnLjm2qTqVFFRgTAACIUoifjte+hTTmtbAOx/VYw9P7 U9/Rj2/fH62VoO9pGambym207S4xaNkX4KqSwlr6WCIweBoJiPb+6RvB516AHM1w5XW6 +z8/CbO+bBBCUskvoKoN8s34Jl4sPjQXtTt1EhNHhegtdZNn1qKZXkZXcaUjRFAs1cQ2 vKCnU0WVujD0Ab1ALvoPsqzcZjOIWD0/pT8l+VuNBcLQ8XZRFDw+51XG77+r2SlA3iBm vki7RVW6BMKpgl449DEHN4VjpWhyqiGUDX99BvUWpReD+yvFL6YvsKDaddICoRIbXpcR QhGg== MIME-Version: 1.0 X-Received: by 10.60.62.241 with SMTP id b17mr1395041oes.17.1421216530838; Tue, 13 Jan 2015 22:22:10 -0800 (PST) Date: Wed, 14 Jan 2015 11:52:10 +0530 Subject: Threading in Python, Please check the script From: Robert Clove To: "python-list@python.org" Content-Type: multipart/alternative; boundary=001a11c21ca8783a0b050c96c2e7 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: 117 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1421216540 news.xs4all.nl 2874 [2001:888:2000:d::a6]:60390 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83734 --001a11c21ca8783a0b050c96c2e7 Content-Type: text/plain; charset=UTF-8 Hi All, 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)) 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 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() print "In try" except: print "Error: unable to start thread" --001a11c21ca8783a0b050c96c2e7 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hi All,

I = have made a script in which i have started two thread named thread 1 and th= read 2.
In thread 1 one function will run named func1 and in threa= d 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 sub= process
import datetime
import os
import thread

thread.star= t_new_thread( print_time, (None, None))
thread.start_new_thread( print_t= ime1, (None, None))
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=A0 os.chdir(final_dir)
=C2=A0=C2=A0=C2=A0 print "IN first th= read"
=C2=A0=C2=A0=C2=A0 proc =3D subprocess.Popen(command,shell=3D= True,stdout=3Dsubprocess.PIPE, stderr=3Dsubprocess.PIPE)
=C2=A0=C2=A0=C2= =A0 proc.wait(70)
=C2=A0=C2=A0=C2=A0 exitflag=3D1
=C2=A0=C2=A0
de= f print_time1(*args):
=C2=A0=C2=A0=C2=A0 print "In second thread&qu= ot;
=C2=A0=C2=A0=C2=A0 global exitflag
=C2=A0=C2=A0=C2=A0 while exitf= lag:
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0 =C2=A0thread.exit()
=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 #proc =3D subprocess.Popen(command1,shell=3D= True,stdout=3Dsubprocess.PIPE, sterr=3Dsubprocess.PIPE)



# Cr= eate two threads as follows
try:
=C2=A0=C2=A0=C2=A0 while (1):
=C2= =A0=C2=A0=C2=A0 =C2=A0=C2=A0 =C2=A0t1=3Dthreading.Thread(target=3Dprint_tim= e)
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0 =C2=A0t1.start()
=C2=A0=C2=A0=C2= =A0 =C2=A0=C2=A0 =C2=A0t2=3Dthreading.Thread(target=3Dprint_time1)
=C2= =A0=C2=A0=C2=A0 =C2=A0=C2=A0 =C2=A0t2=3Dstart()
=C2=A0=C2=A0=C2=A0 =C2= =A0=C2=A0 =C2=A0time.sleep(80)
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0 =C2=A0z = =3D t1.isAlive()
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0 =C2=A0z1 =3D t2.isAlive= ()
=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0 =C2=A0if z:
=C2=A0=C2=A0=C2=A0 =C2= =A0=C2=A0 =C2=A0=C2=A0=C2=A0 =C2=A0z.exit()
=C2=A0=C2=A0=C2=A0 =C2=A0=C2= =A0 =C2=A0if z1:
=C2=A0=C2=A0=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=C2=A0=C2=A0 threadin= g.Thread(target=3Dprint_time1).start()
=C2=A0=C2=A0 =C2=A0=C2=A0 =C2=A0= =C2=A0=C2=A0 =C2=A0threading.Thread(target=3Dprint_time1).start()
=C2=A0= =C2=A0=C2=A0 =C2=A0=C2=A0 =C2=A0print "In try"
except:
=C2= =A0=C2=A0 print "Error: unable to start thread"

=C2=A0
=
--001a11c21ca8783a0b050c96c2e7--