emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
blob 08284c470a745e5b1a9e63333eccacdfe5eb32e4 17149 bytes (raw)
name: scratch/bba-ob-core-async/my-async-tests.org 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
 
#+PROPERTY: HEADER-ARGS+ :eval no-export :exports both
* Intro

An org document with code blocks to help test the proposed patchs.

See [[*On top of ob-shell][On top of ob-shell]] for asynchronous shell scripts using ob-shell.

See [[*On top of ob-python][On top of ob-python]] for asynchronous python scripts using ob-python.

You need to load:
   #+begin_src elisp :results silent
   (setq-local org-confirm-babel-evaluate nil)
   (load-file "my-async-tests.el")
   #+end_src


Emacs and org versions:
   #+begin_src elisp
   (mapcar (lambda (sb) (list sb (symbol-value sb)))
           '(emacs-version org-version))
   #+end_src

   #+RESULTS:
   | emacs-version | 30.0.50 |
   | org-version   | 9.7-pre |

Note that we've disabled eval on export: export doesn't know it needs
to wait for asynchronous results.

* POSIX shells
** A simple bash example
   :PROPERTIES:
    :header-args:bash: :execute-with my-shell-babel :nasync yes
   :END:

The package `my-async-tests.el' contains the function
`my-shell-babel-schedule' to evaluate shell script asynchronously.

The header-args properties above request asynchronous execution for
bash (:nasync yes), and, tells ob-core to use the prefix
`my-shell-babel' when looking for functions to evaluate a source
block. Thus, org will delegate execution to `my-shell-babel-schedule'.
We don't have `my-shell-babel-execute', so, in this case, :nasync must
be yes.

Examples taken from the org mailing list and from worg.

A simple execution:
    #+begin_src bash
      date
    #+end_src

    #+RESULTS:
    : Wed Feb 21 15:56:23 CET 2024

A tricky computation takes some time:
    #+begin_src bash
      sleep 1; date
    #+end_src

    #+RESULTS:
    : Wed Feb 21 15:56:25 CET 2024

An example of a failure:
    #+begin_src bash
      sleepdd 1; false
    #+end_src

    #+RESULTS:

** On top of ob-shell
   :PROPERTIES:
    :header-args:bash:  :execute-with my-org-babel :nasync yes
    :header-args:bash+: :session sh-async
   :END:

   #+begin_src bash
   sleep 1; date
   #+end_src

   #+RESULTS:
   : Wed Feb 21 15:56:42 CET 2024


   #+begin_src bash :results output :session *test* :nasync yes
   cd /tmp
   echo "hello world"
   #+end_src

   #+RESULTS:
   : hello world

   #+begin_src bash :results output
   # comment
   # comment
   #+end_src

   #+RESULTS:

    #+begin_src bash :results output
    # print message
    echo \"hello world\"
    #+end_src

    #+RESULTS:
    : "hello world"

    #+begin_src bash :results output
    echo "hello"
    echo "world"
    #+end_src

    #+RESULTS:
    : hello
    : world


    #+begin_src bash :results output
      echo PID: "$$"
    #+end_src

    #+RESULTS:
    : PID: 22212

    #+begin_src bash :results output
      echo PID: "$$"
    #+end_src

    #+RESULTS:
    : PID: 22212


    #+begin_src bash :results output :session shared
      echo PID: "$$"
      X=5
    #+end_src

    #+RESULTS:
    : PID: 22218

    #+begin_src bash :results output :session shared
      echo PID: "$$"
      echo X was set to "$X"
    #+end_src

    #+RESULTS:
    : PID: 22218
    : X was set to 5

    #+begin_src bash :nasync yes :results value scalar
    echo "Execute session blocks in the background"
    sleep 3
    echo "Using the :async header"
    #+end_src

    #+RESULTS:
    : Execute session blocks in the background
    : Using the :async header
    : 0



    #+name: their-os
    Linux


    #+begin_src bash :results output :shebang #!/usr/bin/env bash :stdin their-os :cmdline RMS :tangle ask_for_os.sh

      # call as ./ask_for_os.sh NAME, where NAME is who to ask

      if [ -z "$1" ]; then
          asked="$USER"
      else
          asked="$1"
      fi

      echo Hi, "$asked"! What operating system are you using?
      read my_os

      if [ "$asked" = "RMS" ]; then
          echo You\'re using GNU/"$my_os"!
      elif [ "$asked" = "Linus" ]; then
          echo You\'re using "$my_os"!
      else
          echo You\'re using `uname -o`!
      fi
    #+end_src

    #+RESULTS:
    : Hi, RMS! What operating system are you using?
    : You're using GNU/Linux!



    #+begin_src bash
    declare -a array

    m=4
    n=3
    for ((i=0; i<m; i++))
    do
        for ((j=0; j<n; j++))
        do
            a[${i},${j}]=$RANDOM
        done
    done
    for ((i=0; i<m; i++))
    do
        for ((j=0; j<n; j++))
        do
            echo -ne "${a[${i},${j}]}\t"
        done
        echo
    done
    #+end_src

    #+RESULTS:
    | 7592 | 13920 | 4911 |
    | 7592 | 13920 | 4911 |
    | 7592 | 13920 | 4911 |
    | 7592 | 13920 | 4911 |


    #+begin_src bash :results list
    declare -a array

    m=4
    n=3
    for ((i=0; i<m; i++))
    do
        for ((j=0; j<n; j++))
        do
            a[${i},${j}]=$RANDOM
        done
    done
    for ((i=0; i<m; i++))
    do
        for ((j=0; j<n; j++))
        do
            echo -ne "${a[${i},${j}]}\t"
        done
        echo
    done
    #+end_src

    #+RESULTS:
    - 17593
      384
      1439
    - 17593
      384
      1439
    - 17593
      384
      1439
    - 17593
      384
      1439

    #+begin_src bash :results file :file my_output.txt
    declare -a array

    m=4
    n=3
    for ((i=0; i<m; i++))
    do
        for ((j=0; j<n; j++))
        do
            a[${i},${j}]=$RANDOM
        done
    done
    for ((i=0; i<m; i++))
    do
        for ((j=0; j<n; j++))
        do
            echo -ne "${a[${i},${j}]}\t"
        done
        echo
    done
    #+end_src

    #+RESULTS:
    [[file:my_output.txt]]


    #+begin_src bash :results output
      cat my_output.txt
    #+end_src

    #+RESULTS:
    : 30132	16194	19934	
    : 30132	16194	19934	
    : 30132	16194	19934	
    : 30132	16194	19934


    #+begin_src bash :results output :dir /ssh:phone: :session none
    if [ ! -e  "foo_file" ];
    then
        echo "foo" > foo_file
        echo "Created foo_file"
    else
        echo "foo_file already exists!"
    fi
    #+end_src

    #+RESULTS:
    : foo_file already exists!


    #+begin_src bash :results output :dir /ssh:phone: :session *remote*
    if [ ! -e  "foo_file" ];
    then
        echo "foo" > foo_file
        echo "Created foo_file"
    else
        echo "foo_file already exists!"
    fi
    #+end_src

    #+RESULTS:
    : Created foo_file


    #+RESULTS:


    #+begin_src bash :results none :session *my-session*
      X=1
    #+end_src

    #+RESULTS:

    #+begin_src bash :results output :session *my-session*
      echo X was set to "$X"
    #+end_src

    #+RESULTS:
    : X was set to 1

    #+begin_src bash :results output :session *another-session*
      echo X was set to "$X"
    #+end_src

    #+RESULTS:
    : X was set to

    #+RESULTS:


    #+begin_src bash :results output
    echo "Hello, world!"
    sleep 3
    echo "Good-bye, cruel World..."
    #+end_src

    #+RESULTS:
    : Hello, world!
    : Good-bye, cruel World...



    #+begin_src bash :var by_two=0  x=3 :session none
      if [ "$by_two" = "0" ]; then
        echo $(($x * 2))
      else
        echo $(($x * 3))
      fi
    #+end_src

    #+RESULTS:
    : 6


    #+begin_src bash :results output :var arr='("apple" "banana" "cherry")
      echo The first element is...
      echo \"${arr[1]}\"
    #+end_src

    #+RESULTS:
    : The first element is...
    : "banana"

*** TODO Doesn't work yet: asynchronous that depends from other blocks
    #+name: multiply_by_2
    #+begin_src bash :var data="" :results output
      echo $(($data * 2))
    #+end_src

    #+RESULTS: multiply_by_2
    : bash: * 2: syntax error: operand expected (error token is "* 2")

    #+begin_src bash :post multiply_by_2(data=*this*)
      echo 3
    #+end_src

    #+results:


* On top of ob-python
   :PROPERTIES:
    :header-args:python:  :execute-with my-org-babel :nasync yes
    :header-args:python+: :session py-async
   :END:

Used =header-args= properties:
   - =:execute-with my-org-babel=:  look for functions with the prefix `my-org-babel' to execute
     blocks (for the asynchronous case use
     `my-org-babel-schedule', and, for the synchronous case
     `my-org-babel-execute'). These functions are defined in [[file:my-async-tests.el]].

   - =:nasync yes=: by default, execute asynchronously (use `my-org-babel-schedule').

   - =:session py-async= by default, use a session named "py-async".

** basic examples
*** async with a session
A very simple test:
    #+begin_src python
      2+3
    #+end_src

    #+RESULTS:
    : 5

Let's import the module time in our session.
    #+begin_src python :results silent
      import time
    #+end_src

    #+RESULTS:

(Yes, =:results silent= needs some work.)



A table that requires some time to compute:
    #+begin_src python
      start = time.time()
      time.sleep(1)
      end = time.time()
      ["%.1fs" % t for t in [start, end, end-start]]
    #+end_src

    #+RESULTS:
    | 1708515666.8s | 1708515667.8s | 1.0s |


An error (click on the error , <mouse-1>, to see the details):
    #+begin_src python
      2/0
    #+end_src

    #+RESULTS:


*** async with no session
   :PROPERTIES:
    :header-args:python+: :session none
   :END:

A very simple test:
    #+begin_src python
      2+3
    #+end_src

    #+RESULTS:
    : 5

Let's import the module time in our session.
    #+begin_src python :results silent
      import time
    #+end_src

    #+RESULTS:

(Yes, =:results silent= needs some work.)



A table that requires some time to compute:
    #+begin_src python
      start = time.time()
      time.sleep(1)
      end = time.time()
      ["%.1fs" % t for t in [start, end, end-start]]
    #+end_src

    #+RESULTS:
    | 1708083470.9s | 1708083471.9s | 1.0s |

Yes, it failed, as expected. "import time" was done in its own
temporary session.  The old result is preserved; the error is display
as an overlay. Click on it to get more info about the error.


Let's fix it, adding the import line:
    #+begin_src python
    import time
    start = time.time()
    time.sleep(1)
    end = time.time()
    ["%.1fs" % t for t in [start, end, end-start]]
    #+end_src

    #+RESULTS:
    | 1708515915.0s | 1708515916.0s | 1.0s |


An error (click on the error , <mouse-1>, to see the details):
    #+begin_src python
      2/0
    #+end_src

    #+RESULTS:



*** sync with a session
   :PROPERTIES:
    :header-args:python+: :session py-sync-session :nasync no
   :END:

A very simple test:
    #+begin_src python
      2+3
    #+end_src

    #+RESULTS:
    : 5

Let's import the module time in our session.
    #+begin_src python :results silent
      import time
    #+end_src

(Yes, =:results silent= needs some work.)



A table that requires some time to compute:
    #+begin_src python
      start = time.time()
      time.sleep(1)
      end = time.time()
      ["%.1fs" % t for t in [start, end, end-start]]
    #+end_src

    #+RESULTS:
    | 1708102997.5s | 1708102998.5s | 1.0s |



An error (click on the error , <mouse-1>, to see the details):
    #+begin_src python
      2/0
    #+end_src

    #+RESULTS:


*** sync with no session
   :PROPERTIES:
    :header-args:python+: :session none :nasync no
   :END:

A very simple test:
    #+begin_src python
      2+3
    #+end_src

    #+RESULTS:
    : 5

Let's import the module time in our session.
    #+begin_src python :results silent
      import time
    #+end_src

(Yes, =:results silent= needs some work.)



A table that requires some time to compute:
    #+begin_src python
      start = time.time()
      time.sleep(1)
      end = time.time()
      ["%.1fs" % t for t in [start, end, end-start]]
    #+end_src

    #+RESULTS:
    | 1708083470.9s | 1708083471.9s | 1.0s |

Yes, that fails (no session), displaying the details in a popup. Let's
fix it:
    #+begin_src python
    import time
    start = time.time()
    time.sleep(1)
    end = time.time()
    ["%.1fs" % t for t in [start, end, end-start]]
    #+end_src

    #+RESULTS:
    | 1708103039.0s | 1708103040.0s | 1.0s |



An error (click on the error , <mouse-1>, to see the details):
    #+begin_src python
      2/0
    #+end_src

    #+RESULTS:


** worg examples

Let's import matplotlib in our session.

    #+begin_src python
    import matplotlib
    import matplotlib.pyplot as plt
    #+end_src

    #+RESULTS:
    : None

A figure in a PDF, asynchronous case.
    #+begin_src python :results file link
    fig=plt.figure(figsize=(3,2))
    plt.plot([1,3,2])
    fig.tight_layout()

    fname = 'myfig-async.pdf'
    plt.savefig(fname)
    fname # return this to org-mode
    #+end_src

    #+RESULTS:
    [[file:myfig-async.pdf]]


A figure in a PDF, synchronous case.
    #+begin_src python :results file link :nasync no
    fig=plt.figure(figsize=(3,2))
    plt.plot([1,3,2])
    fig.tight_layout()

    fname = 'myfig-sync.pdf'
    plt.savefig(fname)
    fname # return this to org-mode
    #+end_src

    #+RESULTS:
    [[file:myfig-sync.pdf]]



A PNG figure, asynchronous case.
    #+begin_src python :results graphics file output :file boxplot.png
      fig=plt.figure(figsize=(3,2))
      plt.plot([1,3,2])
      fig.tight_layout()
      fig
    #+end_src

    #+RESULTS:
    [[file:boxplot.png]]

Same, but using the =:return= keyword.
    #+begin_src python :return "plt.gcf()" :results graphics file output :file boxplot.png
      fig=plt.figure(figsize=(3,2))
      plt.plot([1,3,2])
      fig.tight_layout()
    #+end_src

    #+RESULTS:
    [[file:boxplot.png]]

Same, asynchronous but without a session this time.
    #+begin_src python :return "plt.gcf()" :results graphics file output :file boxplot-no-sess-a-y.png :session none
      import matplotlib
      import matplotlib.pyplot as plt
      fig=plt.figure(figsize=(3,2))
      plt.plot([1,3,2])
      fig.tight_layout()
    #+end_src

    #+RESULTS:
    [[file:boxplot-no-sess-a-y.png]]


Lists are table,
    #+begin_src python
      [1,2,3]
    #+end_src

    #+RESULTS:
    | 1 | 2 | 3 |

unless requested otherwise.
    #+begin_src python :results verbatim
      [1,2,3]
    #+end_src

    #+RESULTS:
    : [1, 2, 3]


Dictionaries are tables too.
    #+begin_src python :results table
      {"a": 1, "b": 2}
    #+end_src

    #+RESULTS:
    | a | 1 |
    | b | 2 |


Let's try the example with Panda.
    #+begin_src python :results none
      import pandas as pd
      import numpy as np
    #+end_src

    #+RESULTS:
    : None

    #+begin_src python :results table
      pd.DataFrame(np.array([[1,2,3],[4,5,6]]),
                   columns=['a','b','c'])
    #+end_src

    #+RESULTS:
    |   | a | b | c |
    |---+---+---+---|
    | 0 | 1 | 2 | 3 |
    | 1 | 4 | 5 | 6 |

And the synchronous case?

    #+begin_src python :results table  :nasync no
      pd.DataFrame(np.array([[1,2,3],[4,5,6]]),
                   columns=['a','b','c'])
    #+end_src

    #+RESULTS:
    |   | a | b | c |
    |---+---+---+---|
    | 0 | 1 | 2 | 3 |
    | 1 | 4 | 5 | 6 |



Without session ?

    #+begin_src python :results table :session none
    pd.DataFrame(np.array([[1,2,3],[4,5,6]]),
                 columns=['a','b','c'])
    #+end_src

    #+RESULTS:
    |   | a | b | c |
    |---+---+---+---|
    | 0 | 1 | 2 | 3 |
    | 1 | 4 | 5 | 6 |

Right, we need to import the libraries (no session).

    #+begin_src python :results table :session none
    import pandas as pd
    import numpy as np
    pd.DataFrame(np.array([[1,2,3],[4,5,6]]),
                 columns=['a','b','c'])
    #+end_src

    #+RESULTS:
    |   | a | b | c |
    |---+---+---+---|
    | 0 | 1 | 2 | 3 |
    | 1 | 4 | 5 | 6 |


** inline examples

    A simple asynchronous inline src_python{3*2} {{{results(=6=)}}}.

    An other one containing a mistake src_python{2/0} {{{results(=6=)}}}
    (click on the error to see the details).


    Some very slow inline asynchronous computations that all run in
    the same session. You need to execute the 3 of them at once. Here
    is the first one src_python[:return "\"OK1\""]{import time;
    time.sleep(5)} {{{results(=OK1=)}}} and a second one
    src_python[:return "\"OK1 bis\""]{import time; time.sleep(5)}
    {{{results(=OK1 bis=)}}} and the third one src_python[:return
    "\"OK2\""]{import time; time.sleep(5)} {{{results(=OK2=)}}}.

    Yes, the previous paragraph is unreadable; it's on purpose, to
    check that ob-core can figure it out.

    Let's repeat, in a more readable way, and making the last one
    synchronous.

    Some very slow inline computations that all run in the same
    session. Here is the first asynchronous one
          src_python[:return"\"OK1\""]{import time; time.sleep(5)} {{{results(=None=)}}}
    and a second one, asynchronous too:
          src_python[:return "\"OK1 bis\""]{import time; time.sleep(5)} {{{results(=OK1 bis=)}}}
    and finally, a third one, synchronous this one:
          src_python[:nasync no :return "\"OK2\""]{import time; time.sleep(5)} {{{results(=OK2=)}}}.

    Note that, once the user executes the last synchronous block, the
    user is blocked until the synchronous execution can start
    (i.e. all previous asynchronous executions are done) and until
    it's done.  The display is updated though, to see the asynchronous
    progress.

debug log:

solving 08284c470 ...
found 08284c470 in https://list.orgmode.org/orgmode/65d61a35.050a0220.5a5dc.fb44@mx.google.com/

applying [1/1] https://list.orgmode.org/orgmode/65d61a35.050a0220.5a5dc.fb44@mx.google.com/
diff --git a/scratch/bba-ob-core-async/my-async-tests.org b/scratch/bba-ob-core-async/my-async-tests.org
new file mode 100644
index 000000000..08284c470

1:297: trailing whitespace.
    : 30132	16194	19934	
1:298: trailing whitespace.
    : 30132	16194	19934	
1:299: trailing whitespace.
    : 30132	16194	19934	
Checking patch scratch/bba-ob-core-async/my-async-tests.org...
Applied patch scratch/bba-ob-core-async/my-async-tests.org cleanly.
warning: 3 lines add whitespace errors.

index at:
100644 08284c470a745e5b1a9e63333eccacdfe5eb32e4	scratch/bba-ob-core-async/my-async-tests.org

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).