Would it be possible to wrap this code up into a class? Then you could write a script to create a class instance for each printer, then run the class functions asynchronously. I used threading (import threading) to fire concurrent processes off on one of my projects.
foreground function launched a webserver, while background function ran a jukebox-like music player:
def background():
Player.Routine1()
def foreground():
main_loop.start()
b = threading.Thread(name='background', target=background)
f = threading.Thread(name='foreground', target=foreground)
b.start()
f.start()
I need an alternative to sleep(sec) because the way this code is written loads the CPU core 100% which reduces printing speed. Adding sleep(1) also reduces print speed. Need a better way for checking _command_received.is_set() please.
I can tell you that Cura and OctoPrint are both written in Python. You could use them for inspiration. OctoPrint is Python 2.7x. I do not know which major Python version Cura is.
You should probably read this too. It mentions importing Event from Threading so that you can define myEvent and say myEvent.wait() which is handy.
I have saturated 8 cores on a 2 or so year old server using Python. It got the job done. I even managed to do it without wasting CPU cycles. I was using some of those multithreading modules. I forget if it was with multiprocessing.pool or what.
Thanks gentlemen. Please notice:
while not _ok_event.is_set():
time.sleep(1) # pass overloads CPU core
The loop in the updateDaemon does not overload.
What actually I need is a sleep of milliseconds, not seconds.
Thanks Craig! The prior print job finished in 3:30 compared to SD 2:35. It was with 0.001 sleep and print to screen. Now 0.01 sleep without print output and things look and sound much better:
I was wondering how changing from 0.001 sleep time to 0.01 sleep time made it perform better and then I realized that you removed a print statement. Yeah. opening up the console and printing a little bit of text is for some reason a performance hog. I don’t know…maybe they need to change console interaction to a separate thread or something in Python. It is mind boggling that it would be that slow at printing text.