@@ -979,7 +979,9 @@ main event loop using ``task.executor``:
979
979
``task.executor(func, *args, **kwargs) ``
980
980
Run the given function in a separate thread. The first argument is the function to be called,
981
981
followed by each of the positional or keyword arguments that function expects. The ``func ``
982
- argument can only be a regular Python function, not a function defined in pyscript.
982
+ argument can only be a regular Python function (eg, defined in an imported module), not a
983
+ function defined in pyscript. The return value of ``task.executor `` is the return value of
984
+ the function ``func ``.
983
985
984
986
See `this section <#avoiding-event-loop-i-o >`__ for more information.
985
987
@@ -1133,8 +1135,10 @@ worry about the details. However, the performance will be much slower that regul
1133
1135
which is typically compiled. Any Python packages you import will run at native, compiled speed.
1134
1136
1135
1137
So if you plan to run large chunks of code in pyscript without needing any of the pyscript-specific
1136
- features, you might consider putting them in a package and importing it instead. That way it will
1137
- run at native compiled speed.
1138
+ features, or you want access to native Python features that aren't supported in pyscript (like
1139
+ ``yield ``, ``open ``, ``read `` or ``write ``), you might consider putting them in a package and
1140
+ importing it instead. That way it will run at native compiled speed and have full access to
1141
+ the native Python language.
1138
1142
1139
1143
One way to do that is in one of your pyscript script files, add this code:
1140
1144
@@ -1422,6 +1426,18 @@ If you forget to use ``task.executor``, you might get this warning from HASS:
1422
1426
causing stability issues. Please report issue to the custom component author for pyscript doing
1423
1427
I/O at custom_components/pyscript/eval.py, line 1583: return func(*args, **kwargs)
1424
1428
1429
+ Currently the built-in functions that do I/O, such as ``open ``, ``read `` and ``write `` are not supported
1430
+ to avoid I/O in the main event loop, and also to avoid security issues if people share pyscripts. Also,
1431
+ the ``print `` function only logs a message, rather than implements the real ``print `` features, such
1432
+ as specifying an output file handle. If you want to do file I/O from pyscript, you have two choices:
1433
+
1434
+ - put the code in a separate native Python module, so that functions like ``open ``, ``read `` and ``write ``
1435
+ are available, and call the function in that module from pyscript using ``task.executor ``. See
1436
+ `Importing <#importing >`__ for how to set Python's ``sys.path `` to import a local Python module.
1437
+ - you could use the ``os `` package (which can be imported by setting ``allow_all_imports ``) and
1438
+ calling the low-level functions like ``os.open `` and ``os.read `` using ``task.executor `` to
1439
+ wrap every function.
1440
+
1425
1441
Here's an example fetching a URL. Inside pyscript, this is the wrong way since it does I/O without
1426
1442
using a separate thread:
1427
1443
@@ -1508,6 +1524,10 @@ Here are some areas where pyscript differs from real Python:
1508
1524
A handful of language features are not supported:
1509
1525
1510
1526
- generators and the ``yield `` statement; these are difficult to implement in an interpreter.
1527
+ - built-in functions that do I/O, such as ``open ``, ``read `` and ``write `` are not supported to avoid
1528
+ I/O in the main event loop, and also to avoid security issues if people share pyscripts. The ``print ``
1529
+ function only logs a message, rather than implements the real ``print `` features, such as specifying
1530
+ an output file handle.
1511
1531
- function decorators, beyond the builtin ones, are not yet supported. If there is interest, support
1512
1532
for function decorators could be added. Additionally, the builtin function decorators aren't
1513
1533
functions that can be called and used in-line. There is a feature request to add this.
0 commit comments