10

basically, I have some code like this:

while True:
   number = int(len(oilrigs)) * 49
   number += money
   time.sleep(1)

In front of this I have a start up screen. However because of this while true loop, it blocks it from running the actual start up screen. Instead, it just displays this.

So how do you put the code in the background?

2
  • Do you have a question? Commented Jul 7, 2016 at 19:55
  • I meant to say, how do you put the code in the background? Commented Jul 7, 2016 at 19:56

1 Answer 1

17

Try multithreading.

import threading

def background():
    while True:
        number = int(len(oilrigs)) * 49
        number += money
        time.sleep(1)

def foreground():
    # What you want to run in the foreground

b = threading.Thread(name='background', target=background)
f = threading.Thread(name='foreground', target=foreground)

b.start()
f.start()
Sign up to request clarification or add additional context in comments.

4 Comments

Great response, however, what does the following mean? b = threading.Thread(name='background', target=background)
That means that you have two different threads running concurrently. This allows for processes to run in parallel. The background thread is running your loop, whereas the foreground thread is running whatever you want in the foreground.
Out of interest, what do 'background' and 'foreground' really mean in this context? Does the foreground thread have any sort of priority over the background because it's called after? Concurrency seems to suggest that they're completely independent of one another?
@TomGreenwood This was ~4 years ago so I don't fully remember, but I believe that I called background as such because it was running an infinite loop. There's no prioritization here or at least no attempt to imply there is prioritization.

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.