Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom function within parallelMap()? #34

Open
jeffbotw opened this issue Feb 26, 2023 · 2 comments
Open

Custom function within parallelMap()? #34

jeffbotw opened this issue Feb 26, 2023 · 2 comments
Labels

Comments

@jeffbotw
Copy link

How to call custom php function?

<?php

require __DIR__ . '/vendor/autoload.php';

use function Amp\ParallelFunctions\parallelMap;
use function Amp\Promise\wait;


function myTestFunction($str){
    //returns test
    return substr($test, 0, -1); 
}

$responses = wait(parallelMap([
    'test1',
    'test2',
    'test3',
], function ($str) {
    return myTestFunction($strt);
}));


var_dump($responses); 
@kelunik
Copy link
Member

kelunik commented Feb 26, 2023

Define your function in a file that's auto-loaded by Composer.

@nightmarebug
Copy link

nightmarebug commented Jul 11, 2023

@kelunik

<?php
require 'vendor/autoload.php';

use function Amp\ParallelFunctions\parallelMap;
use function Amp\Promise\wait;

$arguments = ['argument1', 'argument2', 'argument3'];

$responses = wait(parallelMap($arguments, function ($arg) {
    // Call the custom function based on the argument
    if ($arg === 'argument1') {
        return customFunction1($arg);
    } elseif ($arg === 'argument2') {
        return customFunction2($arg);
    } elseif ($arg === 'argument3') {
        return customFunction3($arg);
    }
}));

create a auto load file that have following code

<?php


function customFunction1($arg) {
    // Simulate a time-consuming operation
    sleep(4);
    return "Result from customFunction1: $arg";
}

function customFunction2($arg) {
    // Simulate a time-consuming operation
    sleep(1);
    return "Result from customFunction2: $arg";
}

function customFunction3($arg) {
    // Simulate a time-consuming operation
    sleep(3);
    return "Result from customFunction3: $arg";
}

but it is waiting for customFunction1 output it is not parallel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants