Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #83746

Re: Threading in Python, Please check the script

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 <cloverobert@gmail.com>
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; '&quot;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 <CAEMktPUaiQrEEhvK4okxfBzAynqZDM8c=a93dAo3cfJX1VcWRw@mail.gmail.com> <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 <cloverobert@gmail.com>
To Dave Angel <davea@davea.name>
Content-Type multipart/alternative; boundary=047d7b3393410903ee050c9ba402
Cc "python-list@python.org" <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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.17714.1421237503.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

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'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
>

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Threading in Python, Please check the script Robert Clove <cloverobert@gmail.com> - 2015-01-14 17:41 +0530

csiph-web