Run Python CGI without waiting - python

I've seen write-ups but am not sure how to apply this to my example. I want to be able to push a button on my webpage, run a piece of python code -- and within the python code it runs another process (executable). The executable takes time, so I want the webpage to not wait until the executable is done running.
I have seen things on daemon and forks, but am not sure how to implement them. I am running an Apache Server with Python CGI on a Windows 8 machine.
Currently, my python code runs and the executable runs fine, but the web page waits until the exe is done processing before updating with an alert window that the "Request has been submitted". Here is my popen code as it is now:
p = subprocess.Popen(['test_sendemail.exe',cmd],shell=False,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
Again, everything works fine here, but I need the python script to end even though the exe is running so that it can update the webpage and move on. (once the exe is done running, it fires an email off with results -- which is why I don't want to wait on the webpage for it to finish).
Any thoughts? Thanks!

Related

Windows Task Scheduler stuck by consent window

I am using Windows Task Scheduler to extract data from Bing Ads API
This process requires providing code on the first run, it automatically opens a browser and require copy code from url to the running program. After that, the process can run successfully without any input.
I set up a .bat file to run python script like python C:\abc.py then used window scheduler to open the .bat file to trigger the script.
I did test it by open .bat file directly. It worked.
However, It did not work by triggering window scheduler. The process is stuck at running status. I doubt that it is stuck at consent window input like it was running the first time.
Is there any way to verify that?
If it's true, Is there a way to solve my problem?

Python scripts involving selenium behaving differently when called from task scheduler, but working as intended when run from spyder or command line

I have created a python program which uses selenium and chromedriver. I cannot successfully run this script (or any others using selenium) from the TaskScheduler in any way. However, it runs perfectly fine and does all tasks I need when I run it from Spyder. It also runs perfectly while logged in when I call it via the command line.
What does the program do when working as intended:
Launches a chrome browser.
Automates clicks and page requests.
Downloads a file.
-does stuff with files that is irrelevant to this post-
What does the program do when called from the TaskScheduler:
Starts chrome but it does not appear (no visible browser, but task manager recognizes the chromedriver and chrome being kicked off and running persistently after the script is called)
All of my clicks are on elements by full xpath so I thought maybe the invisible browser wouldn't break it, but it does indeed fail, never fetching the file download.
Possibly relevant information:
My chromedriver is not on path, but is set via
driver = webdriver.Chrome(r'F:\chromedriver.exe') and this works absolutely fine when run by Spyder or command line.
Task Scheduler input
Action: Start a program
Program/Script: C:\ProgramData\Anaconda3\python.exe
Add arguments (optional): "C:\Users\[My_redacted_name]\.spyder-py3\[Client's_redacted_name]\[redacted_task].py"
What I know: The working directory as suggested in Python script not running in task scheduler does not fix anything.
Running from the command line
C:\ProgramData\Anaconda3\python.exe C:\Users\[My_redacted_name]\.spyder-py3\[Client's_redacted_name]\[redacted_task].py
yields the exact results as intended
No other programs I have made have had this kind of issue, and I have dozens of programs running via TaskScheduler with similar functionality to all the components OTHER than selenium / chromedriver.
I actually have two scripts using selenium that both encounter the same issue when running from command line. Their tasks are more or less the same, so solving ONE should solve the other, but it should be noted that the issue is not unique to a single script, but instead unique to scripts using selenium and running from task scheduler
I also see Selenium - Using Windows Task Scheduler vs. command line and am attempting to see if the single response with 0 votes could help, but I'm not sure if the issue is truly the same given it was for IE and on java.
My initial workaround for running the script on a set schedule was utilizing datetime variables and while True loops, then switched to apscheduler for convenience. Switching back to the workaround again ran the script flawlessly.
Reference code:

sending mouse/keyboard events on remote desktop

So I am trying to set up a Continuous integration environment using Jenkins.
One of the build step requires a series of mouse actions/movements to accomplish a task in Excel. I have already written a python script using the ctypes library to do this.
The script works perfectly fine if I run it either through Jenkins or on the server itself when I am actively logged in to the server using remote desktop connection, but as soon as I minimize/close the connection and then run the script from Jenkins, it seems the mouse events never get executed. Is there something I can add to the script to make this work? Thanks for any help you can provide.

launch a python program on a server

Edit: Please note that it's our first time working with a server, python, and this is my second website, so I know almost nothing about it
We have a project and we need to put our python code on a server
the problem is, we've tried using cgi to run the code and we have two issues:
the program uses Tkinter, and it looks like we can't use program that use Tkinter on a server
when I try to run a script with a loop (our program is a timer, so it will loop), the page loads forever, it only stops loading when the program ends
can we add this python program on a server?
(also, we use a apache server)
You cannot run a tkinter program on a server via CGI. Tkinter and web technologies are incompatible with each other.
Your observation about a program that runs in a loop via CGI is correct -- the program must finish in order for the CGI mechanism to work. The web server will work until that process is finished, and then return what the program returns.

Localhost has stopped updating when various flask/python scripts are run, how do I fix this?

I've been testing out a few .py files with Flask, referring to 127.0.0.1:5000 frequently to see if they're interfacing correctly with the HTML, after running the .py I'm getting the following as normal:
* Running on http://127.0.0.1:5000/
* Restarting with reloader
However, 127.0.0.1:5000 has suddenly stopped updating when scripts are run, remaining as it was after the first time a script was run since my computer has been turned on (restarting the machine is the only way I've found to take a fresh look at my work). To confirm that it's not an issue within .py files or my templates, Flask's hello world example with app.run(debug=True) does not update this localhost page when run. Is there any way to remedy this?
Two things that may or may not be involved with this issue:
(1) I am not using virtualenv but simply running .py files from folder directories on my desktop (following proper format for Flask and the template engine, though). (2) Around the time the problem started I installed SQLAlchemy and its Flash extension, Flask-SQLAlchemy with pip.
After tracking the processes down by running $ netstat -a -o in the command line, it turns out it wasn't a code error, but rather multiple instances of pythonw.exe, which can be taken care of in the Task Manager. I'm not sure why the processes keep running after I close out of all python windows, or why it keeps communicating with 127.0.0.1:5000, however, so thoughts on this would still be appreciated.
Thats right. Just press 'Ctrl+Shift+Esc' to open the task manager.
Scroll down to find the 'python3.exe' files and end the task manually.
The reason is 'ctrl+c' doesnt work for me (it just copies the text on terminal window), so I have to manually kill the python interpreter running in the background. Its hard work, but hey atleast you dont have to restart your computer everytime!!

Categories

Resources