Infinite loop and FreeCAD

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
druneau
Posts: 2
Joined: Sat Oct 29, 2016 9:26 pm

Infinite loop and FreeCAD

Post by druneau »

Hi,

I'm trying to use FreeCAD as a *.brep viewer that automatically re-imports a brep file when it sees the file has changed. However it seems that because the Python console also runs FreeCAD I can't have any infinite loops or else the entire UI hangs.

For example:

Code: Select all

import time
time.sleep(1)
This freezes the UI for 1 second.

In order to monitor the file I had something that ends up being similar to this

Code: Select all

import time
while True:
  #check file and run some FreeCAD commands if required
  time.sleep(1)
This means the UI is completely unresponsive.

I understand why, I'm just not sure how (if possible) to create something that can run an infinite loop without freezing the UI.

Thanks in advance for any help!

Dominique,
UR_
Veteran
Posts: 1355
Joined: Tue Jan 03, 2017 8:42 pm

Re: Infinite loop and FreeCAD

Post by UR_ »

Perhaps you can try something like this:

Code: Select all

import time
import FreeCADGui
import threading

def myloop():
   while FreeCAD.StopMe == False:
     print ("my loop works in background")
     time.sleep(1.)
  


FreeCAD.mythread = threading.Thread(target=myloop)
FreeCAD.StopMe = False
FreeCAD.mythread.start()

# non blocked gui
for i in range (1,200):
    time.sleep(0.1)
    FreeCADGui.updateGui()    

# stop test after 20 sec
FreeCAD.StopMe = True
thread.FCMacro
(427 Bytes) Downloaded 84 times
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: Infinite loop and FreeCAD

Post by TheMarkster »

Try using

QtGui.QApplication.processEvents()

Code: Select all

import time
from PySide import QtCore,QtGui
while True:
  #check file and run some FreeCAD commands if required
    QtGui.QApplication.processEvents()
    time.sleep(.001)
druneau
Posts: 2
Joined: Sat Oct 29, 2016 9:26 pm

Re: Infinite loop and FreeCAD

Post by druneau »

Thank you both for your suggestions.

I've had time to try out the second suggestion and it seems to work for what I'm trying to do. (To be honest I tried the second one because it was only 2 lines to add and I was feeling lazy).
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: Infinite loop and FreeCAD

Post by TheMarkster »

druneau wrote: Sat Jul 21, 2018 7:58 pm Thank you both for your suggestions.

I've had time to try out the second suggestion and it seems to work for what I'm trying to do. (To be honest I tried the second one because it was only 2 lines to add and I was feeling lazy).
That's why I use it, too -- sheer laziness. I'm not sure if it's "true" multitasking, but it serves for most purposes.
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Infinite loop and FreeCAD

Post by Kunda1 »

OP opened issue #7276. Does this merit a ticket? Perhaps we can have a macro syntax reviewer that picks up issues like this and reports them to the user?

"Oh, i see your using:

Code: Select all

time.sleep(1)
this historically locks up the UI while the macro is executing.
Consider perhaps a more friendly method:

Code: Select all

from PySide import QtCore,QtGui
    QtGui.QApplication.processEvents()
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
Post Reply