Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #83734 > unrolled thread
| Started by | Robert Clove <cloverobert@gmail.com> |
|---|---|
| First post | 2015-01-14 11:52 +0530 |
| Last post | 2015-01-14 10:55 -0800 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
Threading in Python, Please check the script Robert Clove <cloverobert@gmail.com> - 2015-01-14 11:52 +0530
Re: Threading in Python, Please check the script sohcahtoa82@gmail.com - 2015-01-14 10:55 -0800
| From | Robert Clove <cloverobert@gmail.com> |
|---|---|
| Date | 2015-01-14 11:52 +0530 |
| Subject | Threading in Python, Please check the script |
| Message-ID | <mailman.17702.1421216540.18130.python-list@python.org> |
[Multipart message — attachments visible in raw view] — view raw
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"
[toc] | [next] | [standalone]
| From | sohcahtoa82@gmail.com |
|---|---|
| Date | 2015-01-14 10:55 -0800 |
| Message-ID | <e834a821-3f05-4a7f-a024-ab30e2e8b6d4@googlegroups.com> |
| In reply to | #83734 |
On Tuesday, January 13, 2015 at 10:22:32 PM UTC-8, Robert Clove wrote:
> 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"
>
>
In addition to what others have said, it looks like you're designing your script to run as root. This is a Very Bad Idea(tm) and has the potential to destroy your system if you make a minor mistake and end up blowing away vital system files.
If you want to be able to run the script anywhere but have the results saved in your home directory, then look into calling os.path.expanduser('~/Desktop') to write to the Desktop directory in your home directory.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web