COROUTINES4ABAP
A coroutine framework for ABAP that launches a piece of work, such as a database query or a background task, as a task the caller awaits later while carrying on with other work.
A caller implements the zif_coroutine_scope interface with the work to run, hands it to zcl_coroutine_manager, which launches it and returns a task, and the caller keeps doing other work until it calls await on that task to get the result back. The two examples in the README cover an asynchronous MARA query and a background email send, both without the caller waiting on them synchronously. It provides the launch-and-await mechanism only: error handling around a launched coroutine, or a fixed limit on how many run at once, is not part of what the README shows.
Facts
Where it sits
Who is behind it
In the atlas: maintains 6, contributes to 8@furkancosgunmaintainer
Before choosing this
Signals
ActivityQuietNo published releases; repository quiet for 26 months
It publishes no releases, and its repository has been quiet for at least 24 full months. This may be normal for stable software; check whether its current state still fits your use.
What it declares
Every artifact
- Type
- Package
- Origin
- Independently maintained
As a package
- Where it runs
- does not say
- Distributed through
- does not say
Connections
Splitting one run's work across several work processes
Download
README
quoted from furkancosgun/COROUTINES4ABAP under MIT, readCOROUTINES FOR ABAP
This project showcases the use of coroutines in ABAP for handling asynchronous operations. Coroutines enable the concurrent execution of tasks, allowing you to perform operations such as database queries and background processing more efficiently. By using coroutines, you can execute tasks asynchronously and manage their completion within your ABAP programs.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contents
Overview
This project provides a framework for implementing coroutines in ABAP. Coroutines are designed to handle multiple concurrent operations, making them useful for optimizing performance and enhancing the responsiveness of your applications. The framework includes methods for starting, managing, and awaiting coroutines, enabling more efficient processing of asynchronous tasks.
Examples
Awaiting Results from a Coroutine
This example demonstrates how to use a coroutine to perform an asynchronous database query and then retrieve the results. The coroutine queries material data from the MARA table based on a specified material number range and returns the results when the operation is completed.
Coroutine Class Definition:
CLASS zcl_example_coroutine DEFINITION.
PUBLIC SECTION.
INTERFACES: zif_coroutine_scope.
TYPES: t_mara TYPE TABLE OF mara WITH DEFAULT KEY.
TYPES: r_matnr TYPE RANGE OF mara-matnr.
METHODS:
constructor IMPORTING iv_matnr TYPE r_matnr,
get_materials RETURNING VALUE(rt_materials) TYPE t_mara.
PRIVATE SECTION.
DATA: mt_materials TYPE t_mara,
mr_matnr TYPE r_matnr.
ENDCLASS.
Coroutine Implementation:
CLASS zcl_example_coroutine IMPLEMENTATION.
METHOD constructor.
me->mr_matnr = iv_matnr.
ENDMETHOD.
METHOD zif_coroutine_scope~launch.
SELECT * FROM mara INTO TABLE me->mt_materials WHERE matnr IN me->mr_matnr.
ENDMETHOD.
METHOD get_materials.
rt_materials = me->mt_materials.
ENDMETHOD.
ENDCLASS.
Main Program:
START-OF-SELECTION.
DATA: lo_task TYPE REF TO zif_coroutine_task,
lo_example_work TYPE REF TO zcl_example_coroutine,
lt_materials TYPE zcl_example_coroutine=>t_mara.
" Create an instance of the coroutine class
lo_example_work = NEW zcl_example_coroutine( iv_matnr = VALUE #( ) ).
" Start the coroutine
lo_task = zcl_coroutine_manager=>run( lo_example_work ).
" Perform other operations
" ...
" Retrieve results from the coroutine
lo_example_work ?= lo_task->await( ).
lt_materials = lo_example_work->get_materials( ).
" Display results
LOOP AT lt_materials INTO DATA(ls_material).
WRITE: / ls_material-matnr, ls_material-meins.
ENDLOOP.
Background Processing with Coroutines
This example illustrates how to use a coroutine for background processing tasks, such as sending an email. The coroutine performs the email sending operation asynchronously, allowing the main program to continue executing without waiting for the email process to complete.
Coroutine Class Definition:
CLASS zcl_example_coroutine DEFINITION.
PUBLIC SECTION.
INTERFACES: zif_coroutine_scope.
METHODS:
constructor IMPORTING iv_recipient TYPE bcs_address,
send_email.
PRIVATE SECTION.
DATA: recipient TYPE bcs_address.
ENDCLASS.
Coroutine Implementation:
CLASS zcl_example_coroutine IMPLEMENTATION.
METHOD constructor.
me->recipient = iv_recipient.
ENDMETHOD.
METHOD zif_coroutine_scope~launch.
send_email( ).
ENDMETHOD.
METHOD send_email.
DATA: lo_msg TYPE REF TO cl_bcs_message,
lt_tstc TYPE TABLE OF tstc.
SELECT * FROM tstc INTO TABLE lt_tstc.
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = DATA(lo_salv)
CHANGING
t_table = lt_tstc
).
CATCH cx_root.
ENDTRY.
lo_msg = NEW #( ).
lo_msg->add_recipient( me->recipient ).
lo_msg->set_subject( 'Example Mail Subj.' ).
lo_msg->set_main_doc( 'Example Mail Content' ).
lo_msg->add_attachment(
EXPORTING
iv_doctype = 'XLS'
iv_filename = 'DOC.xlsx'
iv_contents_bin = lo_salv->to_xml( if_salv_bs_xml=>c_type_xlsx )
).
TRY.
lo_msg->send( ).
CATCH cx_root.
ENDTRY.
ENDMETHOD.
ENDCLASS.
Main Program:
START-OF-SELECTION.
DATA: lo_task TYPE REF TO zif_coroutine_task,
lo_example_work TYPE REF TO zcl_example_coroutine.
" Create an instance of the coroutine class
lo_example_work = NEW zcl_example_coroutine( iv_recipient = 'furkan51cosgun@gmail.com' ).
" Start the coroutine
lo_task = zcl_coroutine_manager=>run( lo_example_work ).
Feel free to customize the code and examples to fit your specific needs.
Others providing
Compare all 6 on the Parallel processing page →- abapify/promiseabapify/promiseA Promise and async/await implementation for ABAP, modelled on JavaScript's Promise API, that runs a resolver as a background task and offers all, race, resolve, reject and all_settled.independent, abapPromise and async/await implementation for ABAPfrom ABAP
- ABAPParallelizationServiceABAPParallelizationServiceA generic framework, called from code, for running a function unit, report or method across many parameter combinations in parallel dialog or batch tasks.independent, abapFramework for running ABAP work units in parallel tasksfrom ABAP
- zconcurrency_apizconcurrency_apiA Java-inspired ABAP library that runs tasks in parallel over SAP's SPTA framework without an RFC-enabled function module, capping concurrent tasks at 40% of the dialog work processes.independent, abapParallel task library for ABAP over the SPTA frameworkfrom ABAP
- RFC_TOOLSRFC_TOOLSA wrapper class for calling asynchronous RFC function modules in parallel from ABAP, one instance per parallel task.independent, abapA wrapper class for calling asynchronous RFC function modules in parallel from ABAP, one instance per parallel task.from ABAP
- zthreadzthreadA threading library for ABAP modelled on Java's Thread and Runnable interface: a runnable class is forked as a thread, joined singly or all at once, and returns a result or a callback.independent, abapThreading library for ABAP modelled on Java's Thread and Runnablefrom ABAP
Details
Sources
Sources are graph entities: the same page may back several entries, and an unreachable source flags every row that rests on it.
Compatibility and provenance
abaplint
History in the atlas
- First seen.
For your agent
- Ref
artifact:furkancosgun-coroutines4abap- JSON
- /artifacts/furkancosgun-coroutines4abap.json
- MCP
- Connecting an agent