I need to run at the same time this two instruction within a python script.
GPIO.output(17,True)
os.system('gphoto2 --capture-image-and-download')
it is important to start them simultaneously.
How can I do that?
As you know - Python is single - threaded. But you can get around this with several ways:
Run multiple Python threads. For example: split your script for to scripts and run them with to different python process simultaneously, like python script1.py in one terminal window and python script2.py in second terminal window.
Use multi-thread module which allows run Python functions in different threads. You can easily find a lot of quick tutorials or just read official docs above.
Also Python allows use multi-process too. The idea is similar like splitting your script on different scripts.
Related
I have 2 py scripts that are sending commands via ethernet to turn on/off logic levels in various electronic components. The GUI's themselves are a very simple interface, however, I am not a python guy. I am able to open up one GUI with Spyder, run the script and control the system just fine, but I need to run 2 scripts simultaneously. Both scripts are similar in nature and operation. Is there a way to run both in Spyder? Or one from two different IDE's? Thank you.
I'm not sure if i fully understand your use case but if the scripts don't take long to execute and when you say run simultaneously you mean at very nearly the same time then you could just write a shell script to execute them consecutively with some timer function if running on a regular schedule. Might need more details to know if that's appropriate. If you need them to run in parallel then that's a different matter.
You'll just be creating a file.sh with something along the lines of:
python script1.py
python script2.py
inside and some logic to run it on whatever schedule you desire
Do they have to run in Spyder? If they are just python scripts you should not need an IDE, just use two command line terminals and run script1.py in one, and script2.py in the other
Hi im a bit new to python but i want to learn more my question is when you build a web application and you are going to use python to do the data handling and calculations does this mean in order for that to be used a terminal,in this case lets say on windows will have to run and that basically listens if and when something was triggered or executed on the python program/script
This article summarizes how different types of Python run. By default, on many systems, CPython is present. When it interprets py files initially, it may create pyc files, which can then run on the CPython virtual machine, described here. The virtual machine and interpreter are also running when you run python on your terminal, if you are using this. As described in the first article however, CPython isn't the only way to run Python.
I was wondering if I need to run two python programs simultaneously, can I open two terminal windows and run each of them at the same time? Will this slow down the computing? If so, is there any way to quickly run several programs at the same time? Like parallel computing. Cheers.
Can I open two terminal windows and run each of them at the same time?
You can do that but they won't run parallelly.
Will this slow down the computing?
If you can run them in a truly parallel way, individual programs should not feel any slower than if they were run sequentially.
However, if you have multiple python scripts that you wish to run simultaneously in the same terminal, you can type the following lines on your bash terminal:
python3 script1.py &
python3 script2.py &
Or if you want to run them from another python script, you can do this:
# script3.py
import subprocess
subprocess.run("python3 script1.py & python3 script2.py", shell=True)
Or if you want to run multiple processes parallelly, you can take a look at python's built in multiprocessing module.
Option1: Check using GNU Screen with vertical split
Option2: Download iTerm2 for macOSX from this link.
I have a Python 2.7 script that among others contains the following piece of code:
import spss
columns = []
spss.StartDataStep()
dataset = spss.Dataset()
for column in dataset.varlist:
columns.append(column.name)
spss.EndDataStep()
print columns
When running this code inside a SPSS syntax (so between BEGIN PROGRAM. and END PROGRAM), it runs as expected and I end up with the variables in the active dataset.
However, when running the same code as part of a script (so from Utilities > Run script...) will return me no results.
It looks as if the SPSS session context is not taken into consideration when running a script.
Is there a way around this problem, or am I doing something wrong?
I don't want to run my code as part of Syntax file, I just want to use vanilla Python scripts.
This is, unfortunately, a complicated issue. I don't think Statistics is working as documented. I will take this up with Development.
It appears that in V24, when you run a Python script via Utilities > Run Script (which is the same as issuing the SCRIPT command), your script is connected to the Statistics Viewer process but not to the Statistics backend (the spssengine process), which is where the data live. There are typically three processes running - the stats.exe process, the spssengine process, and, for Python code, the startx process. Your script can issue commands via the spss.Submit api and can use other spss apis, but they go against a new copy of the backend, so the expected backend context is not present.
To get around this, you can run a trivial program like
begin program.
import ascript
end program.
where ascript.py is a Python module on the Python search path. (You could put these lines in an sps file and use INSERT to execute it, too.)
Another way to approach this would be to run Statistics in external mode. In that mode, you run a Python program that uses SPSS apis but the Python program is on top, and no Statistics user interface appears. You can read about this in the Python scripting help.
An advantage of external mode is that you can use your favorite Python IDE to build and debug your code. That's a big advantage if you are basically a Python person. I use Wing IDE, but any Python IDE should work. You can also set up an alternative IDE as your default by editing the clientscriptingcfg.ini file in the Statistics installation directory. See the scripting help for details. With a tool like Wing, this lets you debug your scripts or other Python code even if run within Statistics.
I have some small python 2.6 scripts built....
Now, I would like run them as seperate processes within a python shell. Each as a seperate process. If one fails to run maybe with its timer, I would like others to continue without killing all scripts.
Should I do this as singleton gui's or combine them into bigger launch pad. My perference would be launch pad type gui....Any ideas?
Its seems that launching scripts out of SciTE, works ok.
Check joblaunch, a shell tool I made for executing interdependent jobs in parallel locally. It has more options.