20tab / UnrealEnginePython

Embed Python in Unreal Engine 4
MIT License
2.77k stars 753 forks source link

Multi Threading #823

Open ahmadhajmosa opened 4 years ago

ahmadhajmosa commented 4 years ago

Dear

I am trying to use Threading with UE4 and Python, but whenever I start the thread UE4 crashes. Could you please help me in this matter? My code is as follows:

 def capture_scene(self):
     tic = time.perf_counter()
     self.pawn.call_function('capturescene')

     toc = time.perf_counter()
     ue.log(toc-tic)

def tick(self, delta_time):
    # get current location

    self.t += delta_time
    if self.t >= 1:
        self.t1 = Thread(target = self.capture_scene)
        self.t1.start()
        ue.log('exccc')
        self.t = 0

I am using UE4 4.19 Mac

dfb commented 4 years ago

Threading in UE4 has a lot of restrictions (Google 'ue4 threading' to learn more) - from another thread you generally can't do anything that would cause a UObject to be created, destroyed, or modified, so calling into the engine code from another thread takes great care or must be avoided altogether.

Typically you'd use separate threads to do non-engine things, such as math-heavy computations (such as for generating a runtime mesh) or I/O.