kore

Kore is a web application platform for writing scalable, concurrent web based processes in C or Python.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit 0abc9b19ffe51be1ed95ce6bb35832cb0fdb24cd
parent 66dd856bdc962b2d2b3c46074a27be4c9c597fcd
Author: Joris Vink <joris@coders.se>
Date:   Sun,  2 May 2021 16:25:46 +0200

simplify the python-async example

Diffstat:
examples/python-async/README.md | 4+++-
examples/python-async/app.py | 28++++++++++++++++++++++++++++
examples/python-async/async_http.py | 49+++++++++++++++++++++++++++++++++++++++++++++++++
examples/python-async/async_lock.py | 39+++++++++++++++++++++++++++++++++++++++
examples/python-async/async_process.py | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
examples/python-async/async_queue.py | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
examples/python-async/async_socket.py | 59+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
examples/python-async/conf/build.conf | 34----------------------------------
examples/python-async/conf/python-async.conf | 28----------------------------
examples/python-async/src/async_http.py | 48------------------------------------------------
examples/python-async/src/async_lock.py | 38--------------------------------------
examples/python-async/src/async_process.py | 71-----------------------------------------------------------------------
examples/python-async/src/async_queue.py | 57---------------------------------------------------------
examples/python-async/src/async_socket.py | 57---------------------------------------------------------
examples/python-async/src/init.c | 25-------------------------
examples/python-async/src/setup.py | 23-----------------------
16 files changed, 308 insertions(+), 382 deletions(-)

diff --git a/examples/python-async/README.md b/examples/python-async/README.md @@ -3,9 +3,11 @@ Kore python async/await examples. This example also shows off the asynchronous HTTP client support and requires libcurl on your machine. +Requires that Kore is built with PYTHON=1 CURL=1 + Run: ``` - $ kodev run + $ kore app.py ``` Test: diff --git a/examples/python-async/app.py b/examples/python-async/app.py @@ -0,0 +1,28 @@ +# +# Copyright (c) 2018 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. +# + +import kore + +import async_http +import async_queue +import async_socket +import async_process +import async_process + +kore.server(ip="127.0.0.1", port="8888", tls=False) +kore.domain("*") + +kore.task_create(async_queue.queue_helper()) diff --git a/examples/python-async/async_http.py b/examples/python-async/async_http.py @@ -0,0 +1,49 @@ +# +# Copyright (c) 2019 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. +# + +# +# Asynchronous HTTP client example. +# + +import kore + +# Handler called for /httpclient +@kore.route("/httpclient", methods=["get"]) +async def httpclient(req): + # Create an httpclient. + client = kore.httpclient("https://kore.io") + + # Do a simple GET request. + print("firing off request") + status, body = await client.get() + print("status: %d, body: '%s'" % (status, body)) + + # Reuse and perform another GET request, returning headers too this time. + status, headers, body = await client.get(return_headers=True) + print("status: %d, headers: '%s'" % (status, headers)) + + # What happens if we post something? + status, body = await client.post(body=b"hello world") + print("status: %d, body: '%s'" % (status, body)) + + # Add some custom headers to our requests. + status, body = await client.get( + headers={ + "x-my-header": "async-http" + } + ) + + req.response(200, b'async done') diff --git a/examples/python-async/async_lock.py b/examples/python-async/async_lock.py @@ -0,0 +1,39 @@ +# +# Copyright (c) 2018 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. +# + +# +# Locking example. +# +# The handler for /lock will grab the shared lock, suspend itself for +# 5 seconds before releasing the lock and responding. +# +# While the lock is held, other requests to /lock will block until it +# is released. + +import kore + +# The shared lock +lock = kore.lock() + +@kore.route("/lock", methods=["get"]) +async def async_lock(req): + # A kore.lock should be used with the "async with" syntax. + async with lock: + # Suspend for 5 seconds. + await kore.suspend(5000) + + # Now respond. + req.response(200, b'') diff --git a/examples/python-async/async_process.py b/examples/python-async/async_process.py @@ -0,0 +1,72 @@ +# +# Copyright (c) 2018 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. +# + +# +# Asynchronous process example. +# +# Wait for the result of an external process asynchronously. +# The handler will execute "/bin/ls" on the current directory and +# read the result. +# + +import kore +import json + +@kore.route("/proc", methods=["get"]) +async def async_proc(req): + # + # You may specify a timeout when creating the kore.proc object. + # If the timeout is reached before the process exits kore will + # raise a TimeoutError exception. + # + # Ex: set timeout to 100ms: + # proc = kore.proc("/bin/ls -lR", 100) + + proc = kore.proc("/bin/ls -lR") + + try: + stdout = "" + + # Read until EOF (None is returned) + while True: + try: + # Read from the process, with an optional 1 second timeout. + # The recv() call will throw a TimeoutError exception if + # the timeout has elapsed before any data was read. + chunk = await proc.recv(1024, 1000) + if chunk is None: + break + except TimeoutError as e: + print("recv() timed out: %s" % e) + continue + stdout += chunk.decode() + + # Reap the process. + retcode = await proc.reap() + + # Respond with the return code + the result as JSON. + payload = { + "retcode": retcode, + "stdout": stdout + } + + data = json.dumps(payload, indent=4) + req.response(200, data.encode()) + except Exception as e: + # If an exception occurs we must kill the process first. + proc.kill() + errmsg = "Exception: %s" % e + req.response(500, errmsg.encode()) diff --git a/examples/python-async/async_queue.py b/examples/python-async/async_queue.py @@ -0,0 +1,58 @@ +# +# Copyright (c) 2018 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. +# + +# +# Asynchronous queue example. +# + +import kore + +# The shared queue. +tq = kore.queue() + +# Entry point for our independent coroutine that is created when kore starts. +async def queue_helper(): + while True: + # Wait for a dictionary to arrive. + obj = await tq.pop() + kore.log(kore.LOG_INFO, "coro(): received %s" % obj) + + # Create a message to send back. + msg = "%d = %s" % (kore.time(), obj["msg"]) + + # Send it on the received queue. + obj["rq"].push(msg) + +@kore.route("/queue", methods=["get"]) +async def async_queue(req): + # Create our own queue. + rq = kore.queue() + + # The dictionary we are going to send. + obj = { + # Receive queue object. + "rq": rq, + "msg": "hello" + } + + # Push it onto the tq queue now, which will wake up the other coroutine. + tq.push(obj) + + # Wait for a response. + response = await rq.pop() + + # Send the response to the client. + req.response(200, response.encode()) diff --git a/examples/python-async/async_socket.py b/examples/python-async/async_socket.py @@ -0,0 +1,59 @@ +# +# Copyright (c) 2018 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. +# + +# +# Simple socket example. +# +# The handler will asynchronously connect to the kore app itself and +# send an GET request to /socket-test and read the response. + +import kore +import socket + +@kore.route("/socket", methods=["get"]) +async def async_socket(req): + # Create the socket using Pythons built-in socket class. + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + + # Set it to nonblocking. + sock.setblocking(False) + + # Create a kore.socket with kore.socket_wrap(). + conn = kore.socket_wrap(sock) + + # Asynchronously connect to 127.0.0.1 port 8888 + await conn.connect("127.0.0.1", 8888) + kore.log(kore.LOG_INFO, "connected!") + + # Now send the GET request + msg = "GET /socket-test HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n" + await conn.send(msg.encode()) + kore.log(kore.LOG_INFO, "request sent!") + + # Read the response. + data = await conn.recv(8192) + kore.log(kore.LOG_INFO, "got response!") + + # Respond with the response from /socket-test. + req.response(200, data) + + conn.close() + +@kore.route("/socket-test", methods=["get"]) +async def socket_test(req): + # Delay response a bit, just cause we can. + await kore.suspend(5000) + req.response(200, b'response from /socket-test') diff --git a/examples/python-async/conf/build.conf b/examples/python-async/conf/build.conf @@ -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. -#} diff --git a/examples/python-async/conf/python-async.conf b/examples/python-async/conf/python-async.conf @@ -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 -} diff --git a/examples/python-async/src/async_http.py b/examples/python-async/src/async_http.py @@ -1,48 +0,0 @@ -# -# Copyright (c) 2019 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. -# - -# -# Asynchronous HTTP client example. -# - -import kore - -# Handler called for /httpclient -async def httpclient(req): - # Create an httpclient. - client = kore.httpclient("https://kore.io") - - # Do a simple GET request. - print("firing off request") - status, body = await client.get() - print("status: %d, body: '%s'" % (status, body)) - - # Reuse and perform another GET request, returning headers too this time. - status, headers, body = await client.get(return_headers=True) - print("status: %d, headers: '%s'" % (status, headers)) - - # What happens if we post something? - status, body = await client.post(body=b"hello world") - print("status: %d, body: '%s'" % (status, body)) - - # Add some custom headers to our requests. - status, body = await client.get( - headers={ - "x-my-header": "async-http" - } - ) - - req.response(200, b'async done') diff --git a/examples/python-async/src/async_lock.py b/examples/python-async/src/async_lock.py @@ -1,38 +0,0 @@ -# -# Copyright (c) 2018 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. -# - -# -# Locking example. -# -# The handler for /lock will grab the shared lock, suspend itself for -# 5 seconds before releasing the lock and responding. -# -# While the lock is held, other requests to /lock will block until it -# is released. - -import kore - -# The shared lock -lock = kore.lock() - -async def async_lock(req): - # A kore.lock should be used with the "async with" syntax. - async with lock: - # Suspend for 5 seconds. - await kore.suspend(5000) - - # Now respond. - req.response(200, b'') diff --git a/examples/python-async/src/async_process.py b/examples/python-async/src/async_process.py @@ -1,71 +0,0 @@ -# -# Copyright (c) 2018 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. -# - -# -# Asynchronous process example. -# -# Wait for the result of an external process asynchronously. -# The handler will execute "/bin/ls" on the current directory and -# read the result. -# - -import kore -import json - -async def async_proc(req): - # - # You may specify a timeout when creating the kore.proc object. - # If the timeout is reached before the process exits kore will - # raise a TimeoutError exception. - # - # Ex: set timeout to 100ms: - # proc = kore.proc("/bin/ls -lR", 100) - - proc = kore.proc("/bin/ls -lR") - - try: - stdout = "" - - # Read until EOF (None is returned) - while True: - try: - # Read from the process, with an optional 1 second timeout. - # The recv() call will throw a TimeoutError exception if - # the timeout has elapsed before any data was read. - chunk = await proc.recv(1024, 1000) - if chunk is None: - break - except TimeoutError as e: - print("recv() timed out: %s" % e) - continue - stdout += chunk.decode() - - # Reap the process. - retcode = await proc.reap() - - # Respond with the return code + the result as JSON. - payload = { - "retcode": retcode, - "stdout": stdout - } - - data = json.dumps(payload, indent=4) - req.response(200, data.encode()) - except Exception as e: - # If an exception occurs we must kill the process first. - proc.kill() - errmsg = "Exception: %s" % e - req.response(500, errmsg.encode()) diff --git a/examples/python-async/src/async_queue.py b/examples/python-async/src/async_queue.py @@ -1,57 +0,0 @@ -# -# Copyright (c) 2018 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. -# - -# -# Asynchronous queue example. -# - -import kore - -# The shared queue. -tq = kore.queue() - -# Entry point for our independent coroutine that is created when kore starts. -async def queue_helper(): - while True: - # Wait for a dictionary to arrive. - obj = await tq.pop() - kore.log(kore.LOG_INFO, "coro(): received %s" % obj) - - # Create a message to send back. - msg = "%d = %s" % (kore.time(), obj["msg"]) - - # Send it on the received queue. - obj["rq"].push(msg) - -async def async_queue(req): - # Create our own queue. - rq = kore.queue() - - # The dictionary we are going to send. - obj = { - # Receive queue object. - "rq": rq, - "msg": "hello" - } - - # Push it onto the tq queue now, which will wake up the other coroutine. - tq.push(obj) - - # Wait for a response. - response = await rq.pop() - - # Send the response to the client. - req.response(200, response.encode()) diff --git a/examples/python-async/src/async_socket.py b/examples/python-async/src/async_socket.py @@ -1,57 +0,0 @@ -# -# Copyright (c) 2018 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. -# - -# -# Simple socket example. -# -# The handler will asynchronously connect to the kore app itself and -# send an GET request to /socket-test and read the response. - -import kore -import socket - -async def async_socket(req): - # Create the socket using Pythons built-in socket class. - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - - # Set it to nonblocking. - sock.setblocking(False) - - # Create a kore.socket with kore.socket_wrap(). - conn = kore.socket_wrap(sock) - - # Asynchronously connect to 127.0.0.1 port 8888 - await conn.connect("127.0.0.1", 8888) - kore.log(kore.LOG_INFO, "connected!") - - # Now send the GET request - msg = "GET /socket-test HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n" - await conn.send(msg.encode()) - kore.log(kore.LOG_INFO, "request sent!") - - # Read the response. - data = await conn.recv(8192) - kore.log(kore.LOG_INFO, "got response!") - - # Respond with the response from /socket-test. - req.response(200, data) - - conn.close() - -async def socket_test(req): - # Delay response a bit, just cause we can. - await kore.suspend(5000) - req.response(200, b'response from /socket-test') diff --git a/examples/python-async/src/init.c b/examples/python-async/src/init.c @@ -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); -} diff --git a/examples/python-async/src/setup.py b/examples/python-async/src/setup.py @@ -1,23 +0,0 @@ -# -# Copyright (c) 2018 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. -# - -import kore - -from async_queue import queue_helper - -# Kore worker started, start the queue helper coroutine. -def kore_worker_configure(): - kore.task_create(queue_helper())