Set python program to SCHED_RR or SCHED_FIFO - python

I am running a python program on Raspberry Pi that I want to set to SCHED_FIFO or SCHED_RR priority. I have found some info online of someone doing this in C/C++ with following code:
struct sched_param param;
param.sched_priority = sched_get_priority_max(SCHED_FIFO);
sched_setscheduler(0, SCHED_FIFO, &param);
https://www.raspberrypi.org/forums/viewtopic.php?t=137341
How to do something like this in python either in code or in terminal assuming this is correct?

How to do something like this in python
The translation is fairly straightforward:
import os
param = os.sched_param(os.sched_get_priority_max(os.SCHED_FIFO))
os.sched_setscheduler(0, os.SCHED_FIFO, param)

Related

Passing values from c# window form to python script

Hello programmers of SO,
I am making a C# interface for a socketing,
The interface is made with C# but the code ressponsable for the actual socketing part is written in Python because writing that in C# Would be insanity,
i used some ironPython to execute the python code:
var engine = Python.CreateEngine();
engine.ExecuteFile(""); //directory is inside the quotes
but i need to pass some values (IP and all that jazz)
anyone know how?
I looked at How do I pass arguments to a Python script with IronPython and it would be too hard to implement so dont ask if that thread answers my question please
You could store the data as a string in some file in c sharp, and you could have a thread in python constantly looking for the information. It would know when the information has been stored in the file if a certain keyword is passed along with it. For example (if you were using a textfile with a location called location)
File.WriteAllText(location, IPAdress + "[IP]");
and here is the code for searching for it
foreach(string datainfo in File.ReadAllText(location).Split(Convert.ToChar("\n"))
{
if(datainfo.Contains("[IP]")
datainfo.Replace("[IP]","");
string Ip = datainfo;
}
I don't know how to deal with files in python but the python equivalent of the csharp code above should work.

Some functions from library works in python shell but not in a script

I'm having trouble getting certain functions from a library called art (https://github.com/sepandhaghighi/art) to run in a script, though they work fine in a shell. The script/commands entered sequentially look like this:
from art import *
randart() <(function fails in script, but succeeds in shell)
tart("test") <(different function, same library, succeeds in both shell and script)
import sys
print(sys.version)
The python version is 3.7.5 for both the shell and the script. The first function does not throw an error when run in a script, but does not give any output. Its desired output is a random ascii_art from a collection. I feel like I'm missing something really simple. Any ideas? The documentation on github reports "Some environments don't support all 1-Line arts", however they are the same python version on the same machine. Are there other portions of the environment that could be the cause?
You need to print randart() while writing in script. Make a habit of using print() for everything while printing. Shell is the place which returns the value by default whereas you need to tell the script window what to do with any name or function.
so use this:
from art import *
print(randart())
in the shell it is implicitly printed ... in a script you must explicitly
print(randart())

Using Jenkins variables/parameters in Python Script with os.path.join

I'm trying to learn how to use variables from Jenkins in Python scripts. I've already learned that I need to call the variables, but I'm not sure how to implement them in the case of using os.path.join().
I'm not a developer; I'm a technical writer. This code was written by somebody else. I'm just trying to adapt the Jenkins scripts so they are parameterized so we don't have to modify the Python scripts for every release.
I'm using inline Jenkins python scripts inside a Jenkins job. The Jenkins string parameters are "BranchID" and "BranchIDShort". I've looked through many questions that talk about how you have to establish the variables in the Python script, but with the case of os.path.join(),I'm not sure what to do.
Here is the original code. I added the part where we establish the variables from the Jenkins parameters, but I don't know how to use them in the os.path.join() function.
# Delete previous builds.
import os
import shutil
BranchID = os.getenv("BranchID")
BranchIDshort = os.getenv("BranchIDshort")
print "Delete any output from a previous build."
if os.path.exists(os.path.join("C:\\Doc192CS", "Output")):
shutil.rmtree(os.path.join("C:\\Doc192CS", "Output"))
I expect output like: c:\Doc192CS\Output
I am afraid that if I do the following code:
if os.path.exists(os.path.join("C:\\Doc",BranchIDshort,"CS", "Output")):
shutil.rmtree(os.path.join("C:\\Doc",BranchIDshort,"CS", "Output"))
I'll get: c:\Doc\192\CS\Output.
Is there a way to use the BranchIDshort variable in this context to get the output c:\Doc192CS\Output?
User #Adonis gave the correct solution as a comment. Here is what he said:
Indeed you're right. What you would want to do is rather:
os.path.exists(os.path.join("C:\\","Doc{}CS".format(BranchIDshort),"Output"))
(in short use a format string for the 2nd argument)
So the complete corrected code is:
import os
import shutil
BranchID = os.getenv("BranchID")
BranchIDshort = os.getenv("BranchIDshort")
print "Delete any output from a previous build."
if os.path.exists(os.path.join("C:\\Doc{}CS".format(BranchIDshort), "Output")):
shutil.rmtree(os.path.join("C:\\Doc{}CS".format(BranchIDshort), "Output"))
Thank you, #Adonis!

Run Python script from Ubuntu cronjob

I am trying to run a simple python script on a VM running Ubuntu 18 hosted on my Synology. The python script simply prints date and time in a file so I can check if it ran or not. It looks like this:
from datetime import datetime
with open('file.txt','a') as file:
file.write('Recorded at: %s\n' % datetime.now())
I made a cronjob that looks like this:
* * * * * /home/anaconda3/bin/python3.7 /home/Documents/crontest.py
I have tried many variations. For instance not writing 3.7 and just simply write 'python'. I tried the default python path /usr/bin/python3.7.
Furthermore I tried to ad the shebang #!/home/anaconda/bin/python3.7 to the script and leaving out the path in the cronjob.
It feels like I am missing something elementary here. I tried many options as posted on Stack and other forums, but none of the options seem to fix my problem.
Using relative links in python scripts is not allowed when running cronjobs. So it works when I write it like this:
from datetime import datetime
with open('/home/Documents/file.txt','a') as file:
file.write('Recorded at: %s\n' % datetime.now())
Furthermore, I used the wrong path to python. I wrote /python3.6 instead of /python3 which I found through typing
which python3
in terminal.

Use of node-python to execute python scripts from web application hosted under python?

I've written python scripts for some numerical analysis topics and so I want to plot them in a nice way using any Java Script library.
So, I got to know about python-node and they show some brief doc info at that site:
var python = require('node-python');
var os = python.import('os');
// nodejs stuff
var path = require('path');
assert(os.path.basename(os.getcwd()) == path.basename(process.cwd()))
However, how could I start to actually do python, import python libraries, run python scripts, etc?
That example show how to use import and call functions.
Try following:
var python = require('node-python');
var script = python.import('your_script_module_name');
script.your_function(argument1, argument2)

Categories

Resources