Answer the question
In order to leave comments, you need to log in
How to run a thread only once?
I am writing a project in java using spring, on the main page I run a script written in Python. The question arises how to make it run this script 1 time so that it cannot be run again from another computer?
while sketching I am doing a class-thread: (ThreadsClass.java)
package com.blrmyfc.ComputerVision;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ThreadsClass extends Thread {
private Process process = null;
public void starting(String command, String script){
ProcessBuilder processBuilder = new ProcessBuilder(command, script);
processBuilder.redirectErrorStream(true);
try {
process = processBuilder.start();
} catch (IOException e) {
e.printStackTrace();
}
InputStream stdout = process.getInputStream();
InputStreamReader isrStdout = new InputStreamReader(stdout);
BufferedReader brStdout = new BufferedReader(isrStdout);
String line = null;
while (true){
try {
if (!((line = brStdout.readLine()) != null)) break;
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(line);
}
int exitVal = 0;
try {
exitVal = process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("выход = "+exitVal);
}
public void exit(){
process.destroyForcibly();
}
public void run(){
String command = "python3";
String script = "core/blr_recognizer/production.py";
starting(command, script);
}
}
private ThreadsClass threadsClass;
public ThreadsClass getThreadsClass(){
return threadsClass;
}
public void setThreadsClass(ThreadsClass threadsClass){
this.threadsClass = threadsClass;
}
@PostMapping("/starting")
public String starting() {
ThreadsClass mainThread = new ThreadsClass();
setThreadsClass(mainThread);
System.out.println(getThreadsClass().getState());
getThreadsClass().start();
return "true";
}
@PostMapping("/interrupt")
public String interruptScript(){
getThreadsClass().exit();
getThreadsClass().interrupt();
return "true";
}
}
Answer the question
In order to leave comments, you need to log in
It is necessary to make some kind of flag in the external system: it can be, for example, some kind of lock file or a record in the database (in SQL or NOSQL)
at the very beginning put
and then scatter over the code
echo 'Метка 1 - '.round( (microtime(true) - TIMESTART), 5).' сек'.PHP_EOL;
.............
echo 'Метка 2 - '.round( (microtime(true) - TIMESTART), 5).' сек'.PHP_EOL;
Look for dumb loops, hosting issues, client bells and whistles issues, external connections that are incorrectly specified.
@uranus235 it's still not the sites to blame, I have a turn, your slow loaded in a split second, and fast 4-5 seconds
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question