I have this code where it loads necessary files and prints necessary information when the server starts but inside if __name__ == "__main__": I'm starting a background process as well then finally app.run() is executed.
My problem is after loading all and comes to the starting of background process it starts to print and load everything from beginning again. And also it does the same when the server get its first request (GET/POST). How can I make it load only once?
import web
from multiprocessing import Process
import scripts
print 'Engine Started'
# Code to load and print necessary stuff goes here...
urls = (
'/test(.*)', 'Test'
)
class Test():
def GET(self,r):
tt = web.input().t
print tt
return tt
if __name__ == "__main__":
try:
print 'Cache initializing...'
p = Process(target=scripts.initiate_cleaner)
p.start() # Starts the background process
except Exception, err:
print "Error initializing cache"
print err
app = web.application(urls, globals())
app.run()
So this loads thrice ('Engine Started' prints thrice) after starting process and requesting from localhost:8080/test?t=er
I went through this but it solves the problem in flask and I use web.py
I'm not sure why this surprises you, or why it would be a problem. A background process is by definition a separate process from the web process; each of those will import the code, and therefore print that message. If you don't want to see that message, put it inside the if __name__ block.
Related
I have long running process, that I want to keep track about in which state it currently is in. There is N processes running in same time therefore multiprocessing issue.
I pass Queue into process to report messages about state, and this Queue is then read(if not empty) in thread every couple of second.
I'm using Spider on windows as environment and later described behavior is in its console. I did not try it in different env.
from multiprocessing import Process,Queue,Lock
import time
def test(process_msg: Queue):
try:
process_msg.put('Inside process message')
# process...
return # to have exitstate = 0
except Exception as e:
process_msg.put(e)
callback_msg = Queue()
if __name__ == '__main__':
p = Process(target = test,
args = (callback_msg,))
p.start()
time.sleep(5)
print(p)
while not callback_msg.empty():
msg = callback_msg.get()
if type(msg) != Exception:
tqdm.write(str(msg))
else:
raise msg
Problem is that whatever I do with code, it never reads what is inside the Queue(also because it never puts anything in it). Only when I switch to dummy version, which runs similary to threading on only 1 CPU from multiprocessing.dummy import Process,Queue,Lock
Apparently the test function have to be in separate file.
I would like to know, Is it possible to run a function after response from web.py service, which function takes long time to run?
Lets say some example as below.
file Name: code.py
import web
import time
urls = (
'/', 'index'
)
app = web.application(urls, globals())
class index:
def GET(self):
try:
with open('filename.txt', 'a') as file:
for i in range(100):
time.sleep(1)
file.write("No of times: {}".format(i))
return "some json response"
except:
return "Exception occurred"
if __name__ == "__main__":
app.run()
When I run the above code, obviously it will take time because as we are using time module for sleep one sec and then write into file. So, I should wait 100 seconds for get the response from service.
I want to skip this 100 seconds waiting time.
Expected: First return response to client and then run this part in background?
Can somebody provide some solution. Thanks..
Have a look python documentation for Thread.run()
Note:
With background task you won't be able to return "Exception occurred" as you're doing now. I believe you're OK with it.
Here's a small easy solution. There are other ways too but I feel you should explore more by yourself since you're a python beginner. :)
import web
import time
urls = (
'/', 'index'
)
app = web.application(urls, globals())
class index:
def writeToFile():
try:
with open('filename.txt', 'a') as file:
for i in range(100):
time.sleep(1)
file.write("No of times: {}".format(i))
# Log completion
except:
# Log error
def GET(self):
thread = Thread(target=writeToFile)
thread.start()
return {<myJSON>}
if __name__ == "__main__":
app.run()
I am using Flask as a local server and initiating a new thread:
rospy.init_node('path_planner')
As I am initiating this thread on a main thread, when I press Ctrl-C, nothing happens and I have to manually kill the process using kill -9
I have tried to signal_handler but still I was not able to kill my program.
Here is my code for POST method, which is used often:
app = Flask(__name__)
app.config["MONGO_URI"] = "mongodb://ed:123#ds029227.mlab.com:2325/test"
mongo = PyMongo(app)
#app.route('/goal', methods=['POST'])
def add_goal():
goal = mongo.db.goal
position = request.json['position']
orientation = request.json['orientation']
goal_id = goal.insert({'position' : position, 'orientation' :
orientation})
new_goal = goal.find_one({'_id' : goal_id})
output = {'position' : new_goal['position'], 'orientation' :
new_goal['orientation']}
position_x = json.loads(position['x'])
position_y = json.loads(position['y'])
return jsonify(output)
And here is my main:
if __name__ == '__main__':
rospy.init_node("path_planner")
app.run(debug=True)
When I run my code, flask server fires up and everything works as expected, POST method does its job. However, when I am done and I need to exit the program, I press Ctrl-C but nothing appears to happen.
try to press the break button. It will break the process I think.
i need to use global variables in my program with either flask or bottle running as a webservice. so far im using bottle as a thread with a snippet i found here: Starting python bottle in a thread/Process and another daemon next to it
i basically want to go to localhost:8080/hello to increase the global variable test by one:
#!/usr/bin/env python
from bottle import route, run
from multiprocessing import Process
#route('/hello')
def hello():
global test
test = test + 1
return test
def main():
global test
test = 0
t = Process(target=bottle.run(host='0.0.0.0', port=8080))
t.daemon = True
t.start()
while(True)
print test
time.sleep(0.5)
if __name__ == "__main__":
main()
if i go to localhost:8080/hello with my browser i get:
Error 500: Internal Server Error - Unhandled Exception
i cant see the exception tho, even with
try
global test
test = test + 1
return test
except Exception e
print e
You're returning an int from hello, but you need to return an iterable (of strings).
Tip: add debug=True to your call to run to get better error info in your response.
t = Process(target=run(host='0.0.0.0', port=8080, debug=True))
I am compiling my Python script into a Windows Executable. The script simply downloads a a files and saves them locally - each download uses a different thread. I am finding that my simple application exits before any of the threads finish. But I am not entirely sure?
Does my script below exit before the threads finish or does the script wait till they are done? AND If the script does exit before the threads finish - How can I stop this?
Whats they standard practice to avoid this? Should I use a while loop that checks if any threads are still alive or is there a standard way of doing this?
import thread
import threading
import urllib2
def download_file():
response = urllib2.urlopen("http://website.com/file.f")
print "Res: " + str(response.read())
raw_input("Press any key to exit...")
def main():
# create thread and run
#thread.start_new_thread (run_thread, tuple())
t = threading.Thread(target=download_file)
t.start()
if __name__ == "__main__":
main()
# The below prints before "Res: ..." which makes me think the script exits before the thread has completed
print("script exit")
What you are looking for is the join() function on your newly created thread, which will block the execution of code until the thread is done. I took the liberty of removing your def main() as it is completely not needed here and only creates confusion.
If you want to wrap the launch of all downloads into a neat function, then pick a descriptive name for it.
import thread
import threading
import urllib2
def download_file():
response = urllib2.urlopen("http://website.com/file.f")
print "Res: " + str(response.read())
raw_input("Press any key to exit...")
if __name__ == "__main__":
t = threading.Thread(target=download_file)
t.start()
t.join()
# The below prints before "Res: ..." which makes me think the script exits before the thread has completed
print("script exit")