I'm learning to use the Batch API.
First, you have to build an array of the parameters you'll need for the batch, then pass it to batch_set():
$batch = array( 'title' => t('Exporting'), 'operations' => array( array('my_function_1', array($account->id(), 'story')), array('my_function_2', array()), ), 'finished' => 'my_finished_callback', 'file' => 'path_to_file_containing_myfunctions', );
This code is from the Batch Operations API page: https://api.drupal.org/api/drupal/core%21includes%21form.inc/group/batch/8.3.x
I wondered about the way the operations functions are defined, and how arguments are passed to them. What if the function you want to run is in your current class file and not in an include? In my case, and I think most others, the code is in a form class. batch_set() is intended to be called from a form submit, and you will have to do a little more work to call it from somewhere else (read the link above).
$batch = array( 'title' => t('Exporting'), 'operations' => array( array(array($this, 'my_function_1'), array($account->id(), 'story')), array(array($this, 'my_function_2'), array()), ), 'finished' => 'my_finished_callback', 'file' => 'path_to_file_containing_myfunctions', );
I found some good info on PHP Callbacks here: http://php.net/manual/en/language.types.callable.php. The change I made was to replace the callback strings 'my_function_1' with an array [$this, 'my_function_1'].
*The example uses the old array syntax - use square brackets in your code: [].
https://www.drupal.org/docs/develop/standards/coding-standards#array
Ok, but don't actually do this
Good, you read this far. What I was originally trying to do was write batch-related functions in my form definition class. This is a bad move because the class file gets loaded for rendering the form, and this code isn't needed then. That's why the batch example shows the file (use like 'file' => drupal_get_path('module', 'MYMODULE') . '/MYFILE.inc').
Just follow the example linked at the top, and put your batch code in a separate file for best results. I only dug into this to learn how PHP callbacks work.