Is there any way to make async python3 like node.js do?
I want a minimal example, I've tried the below, but still works with sync mode.
import urllib.request
class MyHandler(urllib.request.HTTPHandler):
#staticmethod
def http_response(request, response):
print(response.code)
return response
opener = urllib.request.build_opener(MyHandler())
try:
opener.open('http://www.google.com/')
print('exit')
except Exception as e:
print(e)
If the async mode works, the print('exit') should display first.
Can anyone help?
Using threading (based on your own code):
import urllib.request
import threading
class MyHandler(urllib.request.HTTPHandler):
#staticmethod
def http_response(request, response):
print(response.code)
return response
opener = urllib.request.build_opener(MyHandler())
try:
thread = threading.Thread(target=opener.open, args=('http://www.google.com',))
thread.start() #begin thread execution
print('exit')
# other program actions
thread.join() #ensure thread in finished before program terminates
except Exception as e:
print(e)
Related
I start a server and use some data from my function. But I want this function to update data and display new one on my server. However when I start a web server it only takes the first compiled data from function.
I use "schedule" - imported library, that can schedule my task to compile my function in time i choose. Also bottle web framework to start server and make routing.
def read_file():
f=open("345.txt", "r")
hi.contents = f.read()
print(hi.contents)
def server_start():
#route('/as', method = 'GET')
def display_status():
try:
return hi.contents
except Exception:
logging.exception("")
return "Service unavailable. Check logs"
run(host='0.0.0.0', port=8033)
print("sadq")
schedule.every(3).seconds.do(read_file)
server_start()
while True:
schedule.run_pending()
time.sleep(1)
I expect to get updated results on my web server. Would be very glad if you help me or give some good advices. Thak you all.
First I would run bottle with an async process, specifically gevent.
import gevent
from gevent import monkey, signal
monkey.patch_all()
from bottle import app
import scheduler
app = Bottle()
#route('/as', method = 'GET')
def display_status():
try:
return scheduler.contents
except Exception:
logging.exception("")
return "Service unavailable. Check logs"
print("sadq")
server = WSGIServer(("0.0.0.0", int(8083)), app)
def shutdown():
print('Shutting down ...')
server.stop(timeout=60)
exit(signal.SIGTERM)
gevent.signal(signal.SIGTERM, shutdown)
gevent.signal(signal.SIGINT, shutdown) #CTRL C
server.serve_forever()
Then I would launch your scheduler as such in a separate file scheduler.py:
from gevent import spawn, sleep
import schedule
contents = ''
def read_file():
global contents
f=open("345.txt", "r")
contents = f.read()
print(contents)
def start_thread():
while 1:
schedule.run_pending()
sleep(1)
schedule.every(3).seconds.do(read_file)
spawn(start_thread)
I am setting an exception handler on my asyncio event loop. However, it doesn't seem to be called until the event loop thread is stopped. For example, consider this code:
def exception_handler(loop, context):
print('Exception handler called')
loop = asyncio.get_event_loop()
loop.set_exception_handler(exception_handler)
thread = Thread(target=loop.run_forever)
thread.start()
async def run():
raise RuntimeError()
asyncio.run_coroutine_threadsafe(run(), loop)
loop.call_soon_threadsafe(loop.stop, loop)
thread.join()
This code prints "Exception handler called", as we might expect. However, if I remove the line that shuts-down the event loop (loop.call_soon_threadsafe(loop.stop, loop)) it no longer prints anything.
I have a few questions about this:
Am I doing something wrong here?
Does anyone know if this is the intended behaviour of asyncio exception handlers? I can't find anything that documents this, and it seems a little strange to me.
I'd quite like to have a long-running event loop that logs errors happening in its coroutines, so the current behaviour seems problematic for me.
There are a few problems in the code above:
stop() does not need a parameter
The program ends before the coroutine is executed (stop() was called before it).
Here is the fixed code (without exceptions and the exception handler):
import asyncio
from threading import Thread
async def coro():
print("in coro")
return 42
loop = asyncio.get_event_loop()
thread = Thread(target=loop.run_forever)
thread.start()
fut = asyncio.run_coroutine_threadsafe(coro(), loop)
print(fut.result())
loop.call_soon_threadsafe(loop.stop)
thread.join()
call_soon_threadsafe() returns a future object which holds the exception (it does not get to the default exception handler):
import asyncio
from pprint import pprint
from threading import Thread
def exception_handler(loop, context):
print('Exception handler called')
pprint(context)
loop = asyncio.get_event_loop()
loop.set_exception_handler(exception_handler)
thread = Thread(target=loop.run_forever)
thread.start()
async def coro():
print("coro")
raise RuntimeError("BOOM!")
fut = asyncio.run_coroutine_threadsafe(coro(), loop)
try:
print("success:", fut.result())
except:
print("exception:", fut.exception())
loop.call_soon_threadsafe(loop.stop)
thread.join()
However, coroutines that are called using create_task() or ensure_future() will call the exception_handler:
async def coro2():
print("coro2")
raise RuntimeError("BOOM2!")
async def coro():
loop.create_task(coro2())
print("coro")
raise RuntimeError("BOOM!")
You can use this to create a small wrapper:
async def boom(x):
print("boom", x)
raise RuntimeError("BOOM!")
async def call_later(coro, *args, **kwargs):
loop.create_task(coro(*args, **kwargs))
return "ok"
fut = asyncio.run_coroutine_threadsafe(call_later(boom, 7), loop)
However, you should probably consider using a Queue to communicate with your thread instead.
I have a service and my code tries to connect to it with API:
import my_srvice_api
It haven't any mechanism for timeout and my code stays waiting if the service is not running, I want to try and after timeout raise the exception, how can I do that like this:
timeout = 3
try:
obj = my_service_api.connect()
except TimeoutException as e:
print("service my_service start")
I add this decorator (base on this post) and its works. Thanks to all who attended to this question.
import threading
import sys
class TimeoutError(Exception): pass
def timelimit(timeout):
def internal(function):
def internal2(*args, **kw):
class Calculator(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
try:
function(*args, **kw)
except:
pass
c = Calculator()
c.start()
c.join(timeout)
if c.isAlive():
raise TimeoutError
return internal2
return internal
Then I add this decorator to my function:
#timelimit(2)
def time_out_api():
obj = my_service_api.connect()
try:
time_out_api()
except TimeoutError as e:
print("service my_service start")
exit(0)
import gevent.monkey
gevent.monkey.patch_socket()
import requests
from gevent.pool import Pool
import socket
urls = ["http://www.iraniansingles.com"]
def check_urls(urls):
pool = Pool(1)
for url in urls:
pool.spawn(fetch, url)
pool.join()
def fetch(url):
print url
try:
resp = requests.get(url, verify=False, timeout=5.0)
print resp.status_code
except socket.timeout:
print "SocketTimeout"
check_urls(urls)
If I remove the first 2 lines, my program printing SocketTimeout. But with monkeypatch, my program waits forever.
Can someone tell me how to capture that socket timeout exception with monkeypatch?
Problem was gevent default timeout set to None. So we have to set default socket timeout manually.
from gevent import socket
socket.setdefaulttimeout(5)
The code never reaches the print 'never reach1' or print 'never reach2' line when I try to telnet into the server.
import sys, signal
from gevent.server import StreamServer
from gevent.pool import Pool
from gevent import monkey
import gevent
from gevent import Timeout
import random
class SocketPool(object):
def __init__(self):
self.pool = Pool(1000)
def listen(self, socket):
while True:
line = socket.recv(1024)
print line
def add_handler(self, socket, address):
if self.pool.full():
raise Exception("At maximum pool size")
else:
self.pool.spawn(self.listen, socket)
gevent.spawn(self.wait).join()
def wait(self):
try:
timeout = Timeout(1)
timeout.start()
except Timeout:
print 'never reach1'
except:
print 'never reach2'
def shutdown(self): self.pool.kill()
def signal_handler(signal, frame): sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
monkey.patch_all()
sockPool = SocketPool()
server = StreamServer(('127.0.0.1', 5000), sockPool.add_handler)
server.serve_forever()
As you can see from the documentation:
timeout = Timeout(seconds, exception)
timeout.start()
try:
... # exception will be raised here, after *seconds* passed since start() call
finally:
timeout.cancel()
So your try block is completely executed and hence cannot catch the exception.
You should put something like a while loop or a computation that requires at least 1 second to see the exception.
If you want to make a method that simply "sleeps" without using CPU I'd recommend using a simple time.sleep instead of using a Timeout.