is it possible to run a python script list threaded?

So, I have a list I process all parts of each frame

is there any way to use a thread to reduce logic in this instance?

while your at it, make sure I have not done anything silly?

http://www.pasteall.org/60016/python

Bump , this is important for scale of my game,

I found this https://docs.python.org/2/library/multiprocessing.html

but I dont really know how to start.

again, the order of return is not important, only that each object gets proccessed.

What exactly is threading in this case? Is it releated to CPU core threads? Which usually are double of core count.

yes, running multiple proccess all knocking down the same list,

so rather then 0,1, 2, 3, 4,5

its 0,4,8,12,16 (using 4 threads)

Is it CPU dependent on how much threads you can use at once?

Yes, but also python only uses 1 thread at a time,
unless you use threading module, or multiprocessing module.

So it actually uses only 25% of my CPU power in BGE? That’s shame!:frowning: Would it automaticly read thread count and use all of them…

is one thing that i not understand.if is true that we can use many CPU, why not use “50 CPUs weak” (we say with 1GHz each one) ? will be a superpower PC .but i guess is not really so.

PS: will become completely unecessary any sort of optimization … tons of ragdool , open world game a lot … tons of lights

PPS: 24 indentations is really insane :smiley:
(my record should be 13)

Threads vs Multiprocessing:

It’s better to use threads for this case (threading module in python, not multiprocessing), because threads share memory and it is easier to share objects between.

Example


import threading

def worker(variable):
    “”“function to be in a separate thread”“”
    print(“Some threaded stuff: ”, variable)
    return

threads = list()

for i in range(3):
    #target=worker, says that the threading module
    #has to instance the worker function as a new thread
    #in args=(i), you send some arguments, whatever you want to the worker function
    t = threading.Thread(target=worker, args=(i,))

    #This line appends the thread to the thread list we create above
    #to has multiple threads in a global variable
    threads.append(t)

    #this start the thread
    t.start()


Example changed from (In Spanish) http://www.genbetadev.com/python/multiprocesamiento-en-python-threads-a-fondo-introduccion

Documentation:

https://docs.python.org/2/library/threading.html

Marco,
my script processes all assembled components each frame.

so 30 robot × 4 parts = 120 parts to run through that script per frame,

Now, how do I convert my script to a function?

own[‘CompList’] = list of all components.

thank you - @mateosss
btw

I can’t see the video for my internet connection right now, I read it quickly, so it seems that you have a for, that executes code for every object in a list of objects, so all that is inside the for, make it a function, and execute it as in the example above.

Ok this is what I have
but I don’t know exactly what I am doing :stuck_out_tongue:
http://www.pasteall.org/60148/python

That shouldn’t work if i’m not wrong.

What is wrong?

  1. The thread variable is not necessary, but by having it, it allows you to manage the threads as you want for later use. So, the idea of that list called threads has to be a list, and nothing else, that you will append the new threads, and then you have it for later use. So change:
  • threads = own[‘CompList’]
  • threads = list() #to make an empty list
  1. The “for” have to go accross your list of components, not a list of numbers (range(3) is equal to [0, 1, 2]). So change:
  • for i in range(3):
  • for component in own[“CompList”] #lets change the “i” name too for clarity
  1. As you changed I for component change this too:
  • t = threading.Thread(target=componentCheck, args=(i,))
  • t = threading.Thread(target=componentCheck, args=(component))#PD: there is no need for that comma, is only question of tastes

And with that changes it has to work, I have to mention that I never work with threads, I only googled it a little before, so I don’t know it will works perfect, I’m in doubt because I hear of a term called GIL (Global Interpreter Lock), that python has, that locks the limits of threads to 1, for compatibilities reasons with C, but someones says, it doesn’t affect in some cases. So I recommend you to search more about the GIL after you prove this script to see if it is doing something, or it’s the same as before.

Stop running that every frame would be my suggestion.

If you are doing something to an object then do it then and there, don’t assign it to a property to be picked up later.

Threading doesn’t solve inefficient design. Imagine how well Minecraft would run if every block was running a script like that.

-edit-
I’ll give an example:
In your script you deal with the health value and if it’s dropped below 0. This should be dealt with when you are adjusting the health, that way you can remove that entire section from your componentCheck function.

Wait… that runs every frame? that’s not good. You are creating a new thread for every component, 60 times per seconds, that’s not good o.O

1 time * every frame I need to check if the enemy sees you, if his gun is aimed at you, if a lifter is pushing and what angle etc,

the health thing your right about I can move it into ‘impact target’ (the code in the bullets)

but the rest I think should run each frame, 1 time** (tap mode in property trigger)

so what I really want, is to create 4 threads(or 3?) 1 time*

and have each one process 1/4 (or 1/3) of the list each frame

cutting the logic usage of that part of the script 1/3

I use to have an initial script on my game that initializes all the variables and threads in this case. So you have to do so, and you have to plan very well how the threads will be implemented, another thing I realized was that the threads are not being finished. So TODO’s:
1- Investigate well how to close a thread
2- Plan your threads scheme for this system: This is a pretty handy task, because threads are a little world, they have they own execution time, there are asynchronous from your main thread, and may do changes whenever they want to. So you have to make global variables (or you can use properties) and something that checks constantly if some of that variables are changed, to execute an action. Or maybe a system of asymmetric messages between threads, well I don’t know, it is handy, so before start, try doing some tests of trivial code, like prints and so on, and read the documentation, maybe with time you can extend this part of the project to have your own auto-thread module for BGE :slight_smile:
3- Evaluate if this is a priority thing. Because maybe you can do later, and avoid lot of hard work that is like doing modelling or texture painting, it’s a lot of work, but it finally will continue working exacting the same, but with better usage from the processor.
4- Search on google if this is what you have to do!!! Understands that I am talking from what I have seen in the firsts google answers, so I can be totally wrong. Or not. I’m pretty sure this will make your game distribute better the work between your processor’s cores.

Making threads will slow down processing, not speed it up. Pythons GIL will ensure that. You’ll need actual separate processes in the task manager before you can get past that. Even then, it would be quicker to just do the processing of the script than it would to deal with the overhead and delays involved in communicating with other processes, so you aren’t saving time.

Threads in Python should really only be used for I/O bound operations so you can keep processing things while waiting for input/output from disk writing or sockets, etc. CPU bound operations (like yours) run (in most cases) faster in a linear fashion.

-edit-
Are you actually having a framerate problem or is this premature optimisation?