@@ -22,8 +22,10 @@ to WiFi and issue:
22
22
import mip
23
23
mip.install(" github:peterhinch/micropython-async/v3/threadsafe" )
24
24
```
25
- For non-networked targets use ` mpremote ` as described in
26
- [ the official docs] ( http://docs.micropython.org/en/latest/reference/packages.html#installing-packages-with-mpremote ) .
25
+ On any target ` mpremote ` may be used:
26
+ ``` bash
27
+ $ mpremote mip install github:peterhinch/micropython-async/v3/threadsafe
28
+ ```
27
29
28
30
###### [ Main README] ( ../README.md )
29
31
###### [ Tutorial] ( ./TUTORIAL.md )
@@ -47,6 +49,8 @@ For non-networked targets use `mpremote` as described in
47
49
3.1 [ Threadsafe Event] ( ./THREADING.md#31-threadsafe-event )
48
50
3.2 [ Message] ( ./THREADING.md#32-message ) A threadsafe event with data payload.
49
51
4 . [ Taming blocking functions] ( ./THREADING.md#4-taming-blocking-functions ) Enabling uasyncio to handle blocking code.
52
+ 4.1 [ Basic approach] ( ./THREADING.md#41-basic-approach )
53
+ 4.2 [ More general solution] ( ./THREADING,md#42-more-general-solution )
50
54
5 . [ Sharing a stream device] ( ./THREADING.md#5-sharing-a-stream-device )
51
55
6 . [ Glossary] ( ./THREADING.md#6-glossary ) Terminology of realtime coding.
52
56
@@ -188,7 +192,7 @@ thread safe classes offered here do not yet support Unix.
188
192
is only required if mutual consistency of the three values is essential.
189
193
3 . In the absence of a GIL some operations on built-in objects are not thread
190
194
safe. For example adding or deleting items in a ` dict ` . This extends to global
191
- variables which are implemented as a ` dict ` . See [ Globals] ( ./THREADING.md#15-globals ) .
195
+ variables because these are implemented as a ` dict ` . See [ Globals] ( ./THREADING.md#15-globals ) .
192
196
4 . The observations in 1.3 re user defined data structures and ` uasyncio `
193
197
interfacing apply.
194
198
5 . Code running on a core other than that running ` uasyncio ` may block for
@@ -643,7 +647,13 @@ again before it is accessed, the first data item will be lost.
643
647
644
648
Blocking functions or methods have the potential of stalling the ` uasyncio `
645
649
scheduler. Short of rewriting them to work properly the only way to tame them
646
- is to run them in another thread. The following is a way to achieve this.
650
+ is to run them in another thread. Any function to be run in this way must
651
+ conform to the guiedelines above, notably with regard to allocation and side
652
+ effects.
653
+
654
+ ## 4.1 Basic approach
655
+
656
+ The following is a way to "unblock" a single function or method.
647
657
``` python
648
658
async def unblock (func , * args , ** kwargs ):
649
659
def wrap (func , message , args , kwargs ):
@@ -699,6 +709,42 @@ asyncio.run(main())
699
709
```
700
710
###### [ Contents] ( ./THREADING.md#contents )
701
711
712
+ ## 4.1 More general solution
713
+
714
+ This provides a queueing mechanism. A task can assign a blocking function to a
715
+ core even if the core is already busy. Further it allows for multiple cores or
716
+ threads; these are defined as ` Context ` instances. Typical use:
717
+ ``` python
718
+ from threadsafe import Context
719
+
720
+ core1 = Context() # Has an instance of _thread, so a core on RP2
721
+
722
+ def rats (t , n ): # Arbitrary blocking function or method
723
+ time.sleep(t)
724
+ return n * n
725
+
726
+ async def some_task ():
727
+ await core1.assign(rats, t = 3 , n = 99 ) # rats() runs on other core
728
+ ```
729
+ #### Context class
730
+
731
+ Constructor arg:
732
+ * ` qsize=10 ` Size of function queue.
733
+
734
+ Asynchronous method:
735
+ * ` assign(func, *args, **kwargs) ` Accepts a synchronous function with optional
736
+ args. These are placed on a queue for execution in the ` Context ` instance. The
737
+ method pauses until execution is complete, returning the fuction's return
738
+ value.
739
+
740
+ The ` Context ` class constructor spawns a thread which waits on the ` Context `
741
+ queue. The` assign ` method accepts a fuction and creates a ` Job ` instance. This
742
+ includes a ` ThreadSafeFlag ` along with the function and its args. The ` Assign `
743
+ method places the ` Job ` on the queue and waits on the ` ThreadSafeFlag ` .
744
+
745
+ The thread removes a ` Job ` from the queue and executes it. When complete it
746
+ assigns the return value to the ` Job ` and sets the ` ThreadSafeFlag ` .
747
+
702
748
# 5. Sharing a stream device
703
749
704
750
Typical stream devices are a UART or a socket. These are typically employed to
0 commit comments