use threading without classes

B

billiejoex

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.
 
H

Harlin Seritt

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
 
P

Peter Hansen

billiejoex said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,266
Messages
2,571,318
Members
47,998
Latest member
GretaCjy4

Latest Threads

Top