I use time.sleep for lots of reasons
But how do I use a time.sleep on only 1 variable?
Is there a:
import time
time.sleep(j(10)) # <- Focus Here
Without me knowing
Do I have to use another command?
Or is it not available on python at all?
time.sleep will have python pause for the amount of time that you say. It will pause the entire program, so using time.sleep won't work on one variable. I'm assuming you want to maybe stop working on one variable but continuing working with another? Since python goes line by line through your code you would just stop writing code that effects the variable. Then once you want to start working on it again you can start writing code that impacts it again.
Related
I'm writing a game in Rust where each player can submit some python scripts to the server in order to automate various tasks in the game. I plan on using pyo3 to run the python from rust.
However, I can see an issue arising if a player submits a script like this:
def on_event(e):
while True:
pass
Now when the server calls the function (using something like PyAny::call1()) the thread will hang as it reaches the infinite loop.
My first thought was to have pyo3 execute the python one statement at a time, therefore being able to exit if the script been running for over a certain threshold, but I don't think pyo3 supports this.
My next idea was to give each player their own thread to run their own scripts on, that way if one of their scripts got stuck it only affected their gameplay. However, I still have the issue of not being able to kill a thread when it gets stuck in an infinite loop - if a lot of players submitted scripts that just looped, lots of threads would start using a lot of CPU time.
All I need is way to execute python scripts in a way such that if one of them does loop, it does not affect the server's performance at all.
Thanks :)
One solution is to restrict the time that you give each user script to run.
You can do it via PyThreadState_SetAsyncExc, see here for some code. It uses C calls of the interpreter, which you probably can access in Rust (with PyO3 FFI magic).
Another way would be to do it on the OS level: if you spawn a process for the user script, and then kill it when it runs for too long. This might be more secure if you limit what a process can access (with some OS calls), but requires some boilerplate to communicate between the host.
I'm linking a MATLAB code to a DDE application in Excel.
It works fine, but whenever I try to run the code, the operations are done without respecting pauses each time. This part gets paused in the right way, as it should be:
channel = ddeinit('excel','C:\VTScada\NewApplication\Application.xlsx')
%Inserire valori iniziali e finali
s_start = input('s_start','s')
pause(1)
When I run the remaning code, like this for example, it's like MATLAB sums all the values of pauses. Then, after some time, it runs all the code in a very rapid way, without respecting pauses each time.
ddepoke(channel,'r18c2',1)
pause(10)
ddepoke(channel,'r18c2',0)
ddepoke(channel,'r18c2',1)
pause(10)
ddepoke(channel,'r18c2',0)
I tried to solve this problem using the code
pause('on')
pause(10)
pause('off')
but it turns out that in this particular case, MATLAB doesn't respect pauses at all. It seems to worsen the situation. What should I do?
The Matlab pause command means more than just "wait for N seconds"; it interacts with the graphics pipeline and stuff.
This is a hack, but try doing this instead of pause:
java.lang.Thread.sleep(10 * 1000);
That's a lower-level operation that will temporarily stop the program's execution in a more unconditional manner. The * 1000 is there because sleep takes its input in milliseconds instead of seconds.
I solved the problem simpling by removing the '{}' after the if, and adding the 'Java.lang.Thread.sleep()' method, instead of the Pause() method.
Thanks to everyone for the help, in any case
Basically the title, i have a small python script (python 3.9, Windows 10) that i would like to keep running in the background. It is currently listening for keyboard hotkeys and acting like a macro engine. I want it to stay idle, since i use the keyboard module so i doesnt need to call anything, just wait. I tried a while loop with either pass or sleep in it, but both have downsides, the one with pass using a lot of cpu ressources, and the one with sleep being very unreliable.
Is there any pretty/good practice way of doing this ?
I would suggest to use some library that does the trick for you.
For example you can try Asyncio:
import asyncio
loop = asyncio.get_event_loop()
try:
loop.run_forever()
finally:
loop.close()
Here's an Asyncio example for keypress.
I am running a class that takes several minutes. There are many times that it is about halfway done when I realize that I would like to print something. Is there a way to add in a print statement without having to stop the run and add it in if the specific function has not run yet? All I have really been able to find is sleep statements, but those would need to have been added before the run and I can't edit the code using a sleep timer.
You cannot pause the execution of the code and overwrite the code to reflect the changes in the forthcoming execution steps. The python file is compiled into a .pyc file which is currently getting executed. Hence even if you manage to pause it somehow, the current execution will resume the execution of the previous executable itself. also the use of sleep function is not actually stopping the execution. The code is still executing.
A good way to go about doing this is to setup logical breakpoints in your code rather than using multiple print statements throughout. You may have a look at this or this to know how to set break points. Although I would recommend using the VsCode debugger for the task.
Thus in conclusion, you cannot pause the script and make edits such that they reflect upon resuming the script's execution. Technically you can stall the execution of the script but that would never allow you to achieve what you intend to do.
The print statement is not your logger; use logging instead
Here are the docs: https://docs.python.org/3/library/logging.html
Here's a good example on how to use Logging: https://realpython.com/python-logging/
Start putting logger statements throughout your code.
a way to pause the code mid-run
Yes, it's called a breakpoint. You'll need to set a breakpoint somewhere in your code. It can even be a conditional breakpoint in that the breakpoint only triggers when a certain condition is met.
You can then look at the variable values in your program's stack.
PyCharm is very good at all of this. I highly recommend it.
well recently I encountered some freezing in my applications in Long run.
my program uses an infinite while loop to constantly check for new processes from a redis db and if there is any job to work on it will spawns a new process to run it in the background.
so I had issue with its freezing after 20 minutes, sometimes 10 minutes. it took me one week to figure it out that the problem rise from lack of this line before my while loop:
multiprocessing.set_start_method('spawn')
it looks like python does not do that on Windows and since windows does not support fork it's gonna stuck.
anyway, it seems this will solve my problem but I have another question.
in order to make a exe file for this program with something like pyinstaller I need to add another line as below to make sure its not freezing in the exe execution:
multiprocessing.freeze_support()
I want to know does this freeze_support() automatically sets the start method to 'spawn' too? I mean should I use both of these lines or just running one of them is ok? if so which one should I use from now on?
In the case of windows, spawn is already the default method so it would not be necessary to run the set_start_method ('spawn') line of code.
The freeze_support () is a different thing that does not affect the definition of start methods. You must use it in this scenario to generate an .exe.