By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,270 Members | 1,703 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,270 IT Pros & Developers. It's quick & easy.

use threading without classes

P: n/a
Hi all. Hi would like to use two threads in a program but all the examples I
found on the net use oop programming that I doesn't love too much. :-)
Can you code me a short example in wich two different functions are executed
at the same time, plz?

Thank you all.
Aug 31 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Is there any reason why you wouldn't want to do this using the the
threading class? To each his own I suppose. You can try the following
though you'll at least need to use functions:
import time
import thread

def myfunction(string,sleeptime,*args):
while 1:

print string
time.sleep(sleeptime) #sleep for a specified amount of time.

if __name__=="__main__":

thread.start_new_thread(myfunction,("Thread No:1",2))

while 1:pass

Aug 31 '05 #2

P: n/a
billiejoex wrote:
Hi all. Hi would like to use two threads in a program but all the examples I
found on the net use oop programming that I doesn't love too much. :-)
Can you code me a short example in wich two different functions are executed
at the same time, plz?


import time
from threading import Thread

def func1():
print 'first func running'
time.sleep(1)
print 'first func done'

def func2():
print 'second func running'
time.sleep(1)
print 'second func done'

Thread(target=func1).start()
Thread(target=func2).start()

Note that this is still using "OOP programming", and you can't
effectively avoid this in Python without jumping through more hoops than
you're really interested in, but this basically avoids the need to do
OOP things like subclassing.

-Peter
Aug 31 '05 #3

P: n/a
Thank you for your helping.
Aug 31 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.