I want to run a script multiple times sequentially in python - python

I have a python script which basically calls a few functions, but takes quite a while to complete. I want to be able to run this script many times on a loop.
I need the script to wait until it's previous iteration is done.
so for example;
for i in range(times_to_run):
do_things(thing_variable)
How can I make it so that the script waits until do_things() is done, before calling it again, and iterating through the loop. Thanks!

Related

How to make cmds.duplicate() execute immediately when called in maya

How to make cmds.duplicate execute immediately when called in maya? Instead of waiting for the entire script to run and then executing it in batches. For example, for this script below, all execution results will appear immediately after the entire script is executed
import time
for i in range(1, 6):
pm.select("pSphere{}".format(i))
time.sleep(0.5)
cmds.duplicate()
I have tried to use python multithreading, like this
import threading
import time
def test():
for i in range(50):
cmds.duplicate('pSphere1')
time.sleep(0.1)
thread = threading.Thread(target=test)
thread.start()
#thread.join()
Sometimes it can success, but sometimes it will crash maya. If the main thread join, it will not achieve the effect. When I want to do a large number of cmds.duplicate, it will resulting in a very high memory consumption, and the program runs more and more slowly. In addition, all duplicate results appear together after the entire python script runs, so I suspect that when I call cmds When duplicating, Maya did not finish executing and outputting the command, but temporarily put the results in a container with variable capacity. With the increase of my calls, the process of dynamic expansion of the container causes the program to become slower and slower, and the memory consumption also increase dramatically. Because I saw that other plug-ins can see the command execution results in real time, so I thought that this should be a proper way to do this just thath I haven't found yet
Your assumptions are not correct. Maya does not need to display anything to complete a tool. If you want to see the results inbetween you can try to use:
pm.refresh()
but this will not change the behaviour in general. I suppose your memory problems have a different source. You could check if it helps to turn off history or the undo queue temporarily.
And of course Ennakard is right with the answer, that most maya commands are not thread save unless mentioned in the docs. Every node creation and modificatons have to be done in the main thread.
The simple answer is you don't, maya command in general and most interaction with maya are not thread safe
threading is usually used for data manipulation before it get used to manipulate anything in maya, but once you start creating node or setting attribute, or any maya modification, no threading.

Python exit at unspecified point in script

I have a python script and I want to be able to stop it at any time.
If I knew where in the script I may want to stop it I would use quit().
Is there a way to implemet this independent of the current state?
Background: This script runs in a docker container and if I want to stop that container it takes a rather long time to do so and exits with code 137. This bothers me.

queue and scheduling to run functions in python

I have multiple functions in a python script. Each of these functions can be run in 2 to 60 seconds. I want to run all these functions every minute. So, each function should be run every minute.
I use the schedule library in python. But the problem is sometimes it takes more than 60s to run all functions. So, the schedule library misses some functions during the ruuing.
are there any ways that I can create a queue and make sure that all backlog functions will be executed?

How to make an api for a python app that is in a continuous loop?

I have a python app that calls a recursive method which runs forever. It is a loop that scrapes a webpage and looks for a number and once it finds it, it prints out a message, increments the number, and calls the method again with the incremented number. This goes on forever because the webpage updates itself about once a week and my method prints out the message when that update is caught.
I want to make a mobile app that notifies users when the method prints out a message (ideally within a minute or two of the change). What is the best way to create an api that would allow me to do this? If there is another way, how can i do it?
Using recursive method for infinite loop is a big mistake because every time you call method again the last method goes to stack and if you do it infinite time finally you get stack overflow error. best way for infinite jobs are thread with a simple "while True":
import threading
SenderThread = threading.Thread(target=sender)
SenderThread.start()
def sender():
while True:
# do your job here
edit:
according to this:
Python threads are used in cases where the execution of a task involves some waiting. One example would be interaction with a service hosted on another computer, such as a webserver. Threading allows python to execute other code while waiting; this is easily simulated with the sleep function.
The reason i used thread is for the main program to do its job or respose to inputs or any thing else that you need.

How can I check whether the script started with cron job is completed?

I start a script and I want to start second one immediately after the first one is completed successfully?
The problem here is that this script can take 10min or 10hours according to specific cases and I do not want to fix the start of the second script.
Also, I am using python to develop the script, so if you can provide me a solution with python control on the cron it will be OK.
Thank you,
You can use a lock file to indicate that the first script is still running.
You could use atexit module. As part of first script, you could register the function. From the registered function at exit, you could call the second script or execute the second script using system.
import atexit
def at_exit():
print 'invoke the second script'
atexit.register(at_exit)

Categories

Resources