Initial Commit

This commit is contained in:
2024-12-11 22:08:45 +01:00
commit 2d05d854ff
43 changed files with 3610 additions and 0 deletions

8
triotest1/.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

6
triotest1/.idea/git_toolbox_blame.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GitToolBoxBlameSettings">
<option name="version" value="2" />
</component>
</project>

View File

@@ -0,0 +1,84 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="DuplicatedCode" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<Languages>
<language minSize="68" name="Python" />
</Languages>
</inspection_tool>
<inspection_tool class="GrazieInspection" enabled="false" level="TYPO" enabled_by_default="false" />
<inspection_tool class="PyBroadExceptionInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
<inspection_tool class="PyCompatibilityInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ourVersions">
<value>
<list size="1">
<item index="0" class="java.lang.String" itemvalue="3.13" />
</list>
</value>
</option>
</inspection_tool>
<inspection_tool class="PyPackageRequirementsInspection" enabled="false" level="WARNING" enabled_by_default="false">
<option name="ignoredPackages">
<value>
<list size="30">
<item index="0" class="java.lang.String" itemvalue="chardet" />
<item index="1" class="java.lang.String" itemvalue="pymysql" />
<item index="2" class="java.lang.String" itemvalue="certifi" />
<item index="3" class="java.lang.String" itemvalue="rpi.gpio" />
<item index="4" class="java.lang.String" itemvalue="pyserial" />
<item index="5" class="java.lang.String" itemvalue="pika" />
<item index="6" class="java.lang.String" itemvalue="requests" />
<item index="7" class="java.lang.String" itemvalue="urllib3" />
<item index="8" class="java.lang.String" itemvalue="dynaconf" />
<item index="9" class="java.lang.String" itemvalue="idna" />
<item index="10" class="java.lang.String" itemvalue="spidev" />
<item index="11" class="java.lang.String" itemvalue="charset-normalizer" />
<item index="12" class="java.lang.String" itemvalue="django-celery-beat" />
<item index="13" class="java.lang.String" itemvalue="django-celery-results" />
<item index="14" class="java.lang.String" itemvalue="python-dateutil" />
<item index="15" class="java.lang.String" itemvalue="psycopg2-binary" />
<item index="16" class="java.lang.String" itemvalue="reportlab" />
<item index="17" class="java.lang.String" itemvalue="drf-spectacular" />
<item index="18" class="java.lang.String" itemvalue="celery" />
<item index="19" class="java.lang.String" itemvalue="py_expression_eval" />
<item index="20" class="java.lang.String" itemvalue="drf-nested-routers" />
<item index="21" class="java.lang.String" itemvalue="stripe" />
<item index="22" class="java.lang.String" itemvalue="pycountry" />
<item index="23" class="java.lang.String" itemvalue="vat_moss" />
<item index="24" class="java.lang.String" itemvalue="zope-interface" />
<item index="25" class="java.lang.String" itemvalue="gunicorn" />
<item index="26" class="java.lang.String" itemvalue="rpi-gpio" />
<item index="27" class="java.lang.String" itemvalue="django-debug-toolbar" />
<item index="28" class="java.lang.String" itemvalue="pytz" />
<item index="29" class="java.lang.String" itemvalue="ipython" />
</list>
</value>
</option>
</inspection_tool>
<inspection_tool class="PyPep8Inspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="ignoredErrors">
<list>
<option value="E402" />
</list>
</option>
</inspection_tool>
<inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoredIdentifiers">
<list>
<option value="pienosole_login.pienosole_api.libs_v2.mixins.PutAsCreateMixin.*" />
<option value="pienosole_login.pienosole_api.models.Park.chargingstation_set" />
<option value="models.Park.*" />
<option value="pycuda.driver.*" />
</list>
</option>
</inspection_tool>
<inspection_tool class="SpellCheckingInspection" enabled="false" level="TYPO" enabled_by_default="false">
<option name="processCode" value="true" />
<option name="processLiterals" value="true" />
<option name="processComments" value="true" />
</inspection_tool>
<inspection_tool class="SqlDialectInspection" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="SqlNoDataSourceInspection" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="requirements.InstalledPackageInspection" enabled="false" level="WARNING" enabled_by_default="false" />
</profile>
</component>

View File

@@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

7
triotest1/.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="Python 3.12 (triotest1)" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (triotest1)" project-jdk-type="Python SDK" />
</project>

8
triotest1/.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/triotest1.iml" filepath="$PROJECT_DIR$/.idea/triotest1.iml" />
</modules>
</component>
</project>

10
triotest1/.idea/triotest1.iml generated Normal file
View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.12 (triotest1)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

4
triotest1/.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings" defaultProject="true" />
</project>

37
triotest1/echo_client.py Normal file
View File

@@ -0,0 +1,37 @@
import sys
import trio
PORT = 8081
async def sender(client_stream):
print("sender: started!")
while True:
data = b"async can sometimes be confusing, but I believe in you!"
print(f"sender: sending {data!r}")
await client_stream.send_all(data)
await trio.sleep(1)
async def receiver(client_stream):
print("receiver: started!")
async for data in client_stream:
print(f"receiver got data {data!r}")
print("received: connection closed")
sys.exit()
async def parent():
print(f"parent: connecting to 127.0.0.1:{PORT}")
client_stream = await trio.open_tcp_stream("127.0.0.1", PORT)
async with client_stream:
async with trio.open_nursery() as nursery:
print("parent: spawning sender...")
nursery.start_soon(sender, client_stream)
print("parent: spawning receiver...")
# noinspection PyTypeChecker
nursery.start_soon(receiver, client_stream)
if __name__ == '__main__':
trio.run(parent)

42
triotest1/echo_server.py Normal file
View File

@@ -0,0 +1,42 @@
# echo-server.py
import trio
from itertools import count
# Port is arbitrary, but:
# - must be in between 1024 and 65535
# - can't be in use by some other program on your computer
# - must match what we set in our echo client
PORT = 8081
CONNECTION_COUNTER = count()
async def echo_server(server_stream):
# Assign each connection a unique number to make our debug prints easier
# to understand when there are multiple simultaneous connections.
ident = next(CONNECTION_COUNTER)
print(f"echo_server {ident}: started")
try:
async for data in server_stream:
print(f"echo_server {ident}: received data {data!r}")
await server_stream.send_all(data)
print(f"echo_server {ident}: connection closed")
# FIXME: add discussion of (Base)ExceptionGroup to the tutorial, and use
# exceptiongroup.catch() here. (Not important in this case, but important
# if the server code uses nurseries internally.)
except Exception as exc:
# Unhandled exceptions will propagate into our parent and take
# down the whole program. If the exception is KeyboardInterrupt,
# that's what we want, but otherwise maybe not...
print(f"echo_server {ident}: crashed: {exc!r}")
async def main():
await trio.serve_tcp(echo_server, PORT)
# We could also just write 'trio.run(trio.serve_tcp, echo_server, PORT)', but real
# programs almost always end up doing other stuff too and then we'd have to go
# back and factor it out into a separate function anyway. So it's simplest to
# just make it a standalone function from the beginning.
trio.run(main)

66
triotest1/main_server.py Normal file
View File

@@ -0,0 +1,66 @@
from dataclasses import dataclass
import trio
from itertools import count
from functools import partial
CONNECTION_COUNTER = count()
TASK_COUNTER = count()
@dataclass
class Operation:
id: int
sleep_s: int
class Database:
ops: list[Operation]
id_counter: int = 1
def __init__(self):
self.ops = []
@dataclass
class DatabaseHandler:
db: Database
lock: trio.Lock = trio.Lock()
async def connection_handler(dbh: DatabaseHandler, stream):
ident = next(CONNECTION_COUNTER)
async for data in stream:
print(f"{ident}: received data {data!r}")
await stream.send_all(data)
async def op_execution(op: Operation):
task_id = next(TASK_COUNTER)
print(f"Joe {op.id} {task_id} - Started sleeping for {op.sleep_s}sec...")
await trio.sleep(op.sleep_s)
print(f"Joe {op.id} {task_id} - Slept well!")
async def op_spawner(dbh: DatabaseHandler, nursery: trio.Nursery):
while True:
async with dbh.lock:
for op in dbh.db.ops:
nursery.start_soon(op_execution, op)
await trio.sleep(10)
async def main():
db = Database()
for _ in range(5):
i = db.id_counter
db.id_counter += 1
db.ops.append(Operation(i, i + 1))
dbh = DatabaseHandler(db)
async with trio.open_nursery() as nursery:
nursery.start_soon(op_spawner, dbh, nursery)
await trio.serve_tcp(partial(connection_handler, dbh), 3000, host='0.0.0.0')
if __name__ == '__main__':
trio.run(main)