simplify the python-async example

This commit is contained in:
Joris Vink 2021-05-02 16:25:46 +02:00
parent 66dd856bdc
commit 0abc9b19ff
10 changed files with 18 additions and 92 deletions

View File

@ -3,9 +3,11 @@ Kore python async/await examples.
This example also shows off the asynchronous HTTP client support This example also shows off the asynchronous HTTP client support
and requires libcurl on your machine. and requires libcurl on your machine.
Requires that Kore is built with PYTHON=1 CURL=1
Run: Run:
``` ```
$ kodev run $ kore app.py
``` ```
Test: Test:

View File

@ -16,8 +16,13 @@
import kore import kore
from async_queue import queue_helper import async_http
import async_queue
import async_socket
import async_process
import async_process
# Kore worker started, start the queue helper coroutine. kore.server(ip="127.0.0.1", port="8888", tls=False)
def kore_worker_configure(): kore.domain("*")
kore.task_create(queue_helper())
kore.task_create(async_queue.queue_helper())

View File

@ -21,6 +21,7 @@
import kore import kore
# Handler called for /httpclient # Handler called for /httpclient
@kore.route("/httpclient", methods=["get"])
async def httpclient(req): async def httpclient(req):
# Create an httpclient. # Create an httpclient.
client = kore.httpclient("https://kore.io") client = kore.httpclient("https://kore.io")

View File

@ -28,6 +28,7 @@ import kore
# The shared lock # The shared lock
lock = kore.lock() lock = kore.lock()
@kore.route("/lock", methods=["get"])
async def async_lock(req): async def async_lock(req):
# A kore.lock should be used with the "async with" syntax. # A kore.lock should be used with the "async with" syntax.
async with lock: async with lock:

View File

@ -25,6 +25,7 @@
import kore import kore
import json import json
@kore.route("/proc", methods=["get"])
async def async_proc(req): async def async_proc(req):
# #
# You may specify a timeout when creating the kore.proc object. # You may specify a timeout when creating the kore.proc object.

View File

@ -36,6 +36,7 @@ async def queue_helper():
# Send it on the received queue. # Send it on the received queue.
obj["rq"].push(msg) obj["rq"].push(msg)
@kore.route("/queue", methods=["get"])
async def async_queue(req): async def async_queue(req):
# Create our own queue. # Create our own queue.
rq = kore.queue() rq = kore.queue()

View File

@ -23,6 +23,7 @@
import kore import kore
import socket import socket
@kore.route("/socket", methods=["get"])
async def async_socket(req): async def async_socket(req):
# Create the socket using Pythons built-in socket class. # Create the socket using Pythons built-in socket class.
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@ -51,6 +52,7 @@ async def async_socket(req):
conn.close() conn.close()
@kore.route("/socket-test", methods=["get"])
async def socket_test(req): async def socket_test(req):
# Delay response a bit, just cause we can. # Delay response a bit, just cause we can.
await kore.suspend(5000) await kore.suspend(5000)

View File

@ -1,34 +0,0 @@
# python-async build config
# You can switch flavors using: kodev flavor [newflavor]
# Set to yes if you wish to produce a single binary instead
# of a dynamic library. If you set this to yes you must also
# set kore_source together with kore_flavor.
single_binary=yes
kore_source=../../
kore_flavor=PYTHON=1 CURL=1 NOTLS=1 DEBUG=1
# The flags below are shared between flavors
cflags=-Wall -Wmissing-declarations -Wshadow
cflags=-Wstrict-prototypes -Wmissing-prototypes
cflags=-Wpointer-arith -Wcast-qual -Wsign-compare
cxxflags=-Wall -Wmissing-declarations -Wshadow
cxxflags=-Wpointer-arith -Wcast-qual -Wsign-compare
# Mime types for assets served via the builtin asset_serve_*
#mime_add=txt:text/plain; charset=utf-8
#mime_add=png:image/png
#mime_add=html:text/html; charset=utf-8
dev {
# These flags are added to the shared ones when
# you build the "dev" flavor.
cflags=-g
cxxflags=-g
}
#prod {
# You can specify additional flags here which are only
# included if you build with the "prod" flavor.
#}

View File

@ -1,28 +0,0 @@
# python-async configuration
server notls {
tls no
bind 127.0.0.1 8888
}
python_path src
python_import ./src/setup.py
python_import ./src/async_lock.py
python_import ./src/async_queue.py
python_import ./src/async_process.py
python_import ./src/async_socket.py
python_import ./src/async_http.py
domain * {
attach notls
route /queue async_queue
route /lock async_lock
route /proc async_proc
route /socket async_socket
route /socket-test socket_test
route /httpclient httpclient
}

View File

@ -1,25 +0,0 @@
/*
* Copyright (c) 2020 Joris Vink <joris@coders.se>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <kore/kore.h>
#include <kore/hooks.h>
/* Let kore handle the default option parsing. */
void
kore_parent_configure(int argc, char **argv)
{
kore_default_getopt(argc, argv);
}