12
$\begingroup$

The Blender game engine does not move into the next frame if all the active scripts have not yet finished.

Say we have an HTTP script in the game. The script retrieves data from a server. I presume the retrieval process cannot be split into multiple small processes.

I know only of the subprocess module's ability to work outside the bge, but on Windows a new Blender window pops up every time the subprocess module is used to start any new Python script.

$\endgroup$
2
  • $\begingroup$ Have you tried the multiprocess module? $\endgroup$
    – wchargin
    Commented Aug 18, 2013 at 0:58
  • $\begingroup$ Running multiprocessing.Process(time.sleep(3)) will freeze the frame. If you can give an example that functions in Blender, please do. $\endgroup$
    – Rakholl
    Commented Aug 18, 2013 at 17:21

2 Answers 2

8
+50
$\begingroup$

The threading module should take care of it. Just create a separate python thread and run your blocking code (e.g. networking code) in there.

import time
import threading

def main():
    thread = Worker(None)
    thread.start()

class Worker(threading.Thread):

    def __init__(self, args):
        self.args = args
        threading.Thread.__init__(self)

    def run(self):
        print("Starting worker")
        ...
        print("Finishing worker")

Note that Python 3's threading model does not map to the OS threads, all the threads will run under the Python interpreter anyway. Because of this, threading is only useful for executing blocking operations, not for performance gain.

$\endgroup$
2
  • $\begingroup$ Combining your solution with the Popen() from the subprocess module works. Thank you for the help. $\endgroup$
    – Rakholl
    Commented Sep 1, 2013 at 14:16
  • $\begingroup$ @Rakholl If you feel that this answer sufficiently answers your question, consider awarding it your bounty. $\endgroup$
    – wchargin
    Commented Sep 2, 2013 at 3:49
1
$\begingroup$

Or simpler


import threading

def to_run(): # should not take arguments.
    ... # code to execute in the thread (can be a loop, ...) 
        # it uses the same variables as in the main thread.

new_thread = threading.Thread() # create a new thread object.
new_thread.run = to_run
new_thread.start() # the new thread is created and then is running.
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .