7

All the examples I've been able to get to don't really address my problem, of having a certain procedure in the background constantly looping, while the rest of the program continues.

Here is a simple example of a method that works, using _thread:

import _thread
import time


def countSeconds():
    time.sleep(1)
    print("Second")
    _thread.start_new(countSeconds, ())

def countTenSeconds():
    time.sleep(10)
    print("Ten seconds passed")
    _thread.start_new(countTenSeconds, ())


_thread.start_new(countSeconds, ())
_thread.start_new(countTenSeconds, ())

Ignoring the obvious fact that we could track the seconds, and just print something different if it's a multiple of ten, how would I go about creating this more efficiently.

In my actual program, the threading seems to be guzzling RAM, I assume from creating multiple instance of the thread. Do I have to "start_new" thread at the end of each procedure?

Thanks for any help.

3
  • Re-calling a thread in itself doesn't seem really reasonable, maybe something like this is what you need: stackoverflow.com/questions/12435211/… Commented Jan 12, 2016 at 16:59
  • Is there any reason you're not using the threading module? It will be simpler than using the low-level _thread module. Also, thanks to the GIL, threaded Python applications are not actually parallel - you need multiprocessing for that. Commented Jan 12, 2016 at 17:03
  • i agree with @skrrgwasme. I would be using threading (or asyncio). Furthermore, this is the whole objective of threading (run multiple threads [tasks] concurrently within a single process)... Commented Nov 22, 2023 at 14:55

2 Answers 2

8

All the examples I've been able to get to don't really address my problem Which examples?

This does work for me.

import threading
import time

def f():
    time.sleep(1)
    print "Function out!"

t1 = threading.Thread(target=f)

print "Starting thread"
t1.start()
time.sleep(0.1)
print "Something done"
t1.join()
print "Thread Done"

You're asking for a repeated thread, I don't get what exactly you need, this might work:

import threading
import time

var = False
def f():
    counter = 0
    while var:
        time.sleep(0.1)
        print "Function {} run!".format(counter)
        counter+=1

t1 = threading.Thread(target=f)

print "Starting thread"
var = True
t1.start()
time.sleep(3)
print "Something done"
var = False
t1.join()
print "Thread Done"
Sign up to request clarification or add additional context in comments.

1 Comment

The problem with this example is that it is not recursive. It is running the function once, and then quitting.
4

use the threading.timer to continue launching a new background thread

import threading
import time


def countSeconds():
    print("Second")
    threading.Timer(1, countSeconds).start()

def countTenSeconds():
    print("Ten seconds passed")
    threading.Timer(10, countTenSeconds).start()


threading.Timer(1, countSeconds).start()
threading.Timer(10, countTenSeconds).start()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.