• Latest
  • Trending
How to Run Python Functions in Parallel Using Ray API

How to Run Python Functions in Parallel Using Ray API

March 10, 2022
Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023
Vice President Dr. Bawumia inaugurates  ICT Hub

Vice President Dr. Bawumia inaugurates ICT Hub

April 2, 2023
Co-Creation Hub’s edtech accelerator puts $15M towards African startups

Co-Creation Hub’s edtech accelerator puts $15M towards African startups

February 20, 2023
Data Leak Hits Thousands of NHS Workers

Data Leak Hits Thousands of NHS Workers

February 20, 2023
EU Cybersecurity Agency Warns Against Chinese APTs

EU Cybersecurity Agency Warns Against Chinese APTs

February 20, 2023
How Your Storage System Will Still Be Viable in 5 Years’ Time?

How Your Storage System Will Still Be Viable in 5 Years’ Time?

February 20, 2023
The Broken Promises From Cybersecurity Vendors

Cloud Infrastructure Used By WIP26 For Espionage Attacks on Telcos

February 20, 2023
Instagram and Facebook to get paid-for verification

Instagram and Facebook to get paid-for verification

February 20, 2023
YouTube CEO Susan Wojcicki steps down after nine years

YouTube CEO Susan Wojcicki steps down after nine years

February 20, 2023
Inaugural AfCFTA Conference on Women and Youth in Trade

Inaugural AfCFTA Conference on Women and Youth in Trade

September 6, 2022
  • Consumer Watch
  • Kids Page
  • Directory
  • Events
  • Reviews
Wednesday, 29 April, 2026
  • Login
itechnewsonline.com
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion
Subscription
Advertise
No Result
View All Result
itechnewsonline.com
No Result
View All Result

How to Run Python Functions in Parallel Using Ray API

Build distributed applications

by ITECHNEWS
March 10, 2022
in Data Science, Leading Stories
0 0
0
How to Run Python Functions in Parallel Using Ray API

There are many ways to run functions in parallel, but there might be many caveats to deal with. We can overcome all those caveats by using Ray is, a simple, universal API for building distributed applications.

What Ray can do for you:

YOU MAY ALSO LIKE

French Telco Orange Hit by Cyber-Attack

ATC Ghana supports Girls-In-ICT Program

  • Providing simple primitives for building and running distributed applications.
  • Enabling end-users to parallelize single machine code with little to zero code changes.
  • Including a large ecosystem of applications, libraries, and tools on top of the core Ray to enable complex applications.

How to install Ray:

sudo pip3 install Ray

A basic Ray example:

#!/usr/bin/env python3
import ray
@ray.remote
def func1():
from datetime import datetime
import time
now = datetime.now()
current_time = now.strftime(“%H:%M:%S”)
print(“func 1 Start Time =”, current_time)
time.sleep(2)
now = datetime.now()
current_time = now.strftime(“%H:%M:%S”)
print(“func 1 End Time =”, current_time)
@ray.remote
def func2():
from datetime import datetime
import time
now = datetime.now()
current_time = now.strftime(“%H:%M:%S”)
print(“func 2 Start Time =”, current_time)
time.sleep(2)
now = datetime.now()
current_time = now.strftime(“%H:%M:%S”)
print(“func 2 End Time =”, current_time)
if __name__ == ‘__main__’:
ray.init()
ray.get([func1.remote(), func2.remote()])
view rawray hosted with ❤ by GitHub
  • Line 2 imports ray into our project
  • @ray.remote decorator is used to define which functions will run in parallel
  • Line 31 ray.init() initializes Ray
  • Line 32 ray.get([func1.remote(),func2.remote()]) the list of functions to run in parallel

Ray provides a very nice output of the executing functions

./test1.py
(func2 pid=1596) func 2 Start Time = 11:51:48
(func1 pid=1591) func 1 Start Time = 11:51:48
(func2 pid=1596) func 2 End Time = 11:51:50
(func1 pid=1591) func 1 End Time = 11:51:50

This is a very basic example of Ray that executes functions in parallel, those functions do not return anything, let’s see a more advanced example of parallel execution with functions that accept parameters and return results

#!/usr/bin/env python3
import ray
@ray.remote
def func1(x):
return x+1
@ray.remote
def func2(y):
return y+1
if __name__ == ‘__main__’:
ray.init()
ret1, ret2 = ray.get([func1.remote(x=2), func2.remote(y=2)])
print(ret1)
print(ret2)
view rawray_with_return.py hosted with ❤ by GitHub
  • Line 14 —u sing ray.get we execute the functions in parallel with parameters, and they return their results in variables ret1 and ret2.

ray.get function accepts functions as parameters in a list form, also returns results as a list, this means that we can do the following which can be very useful when the number of functions to be executed is varied each time.

#!/usr/bin/env python3
import ray
@ray.remote
def func1(x):
return x+1
@ray.remote
def func2(y):
return y+1
if __name__ == ‘__main__’:
ray.init()
funcs = []
funcs.append(func1.remote(x=2))
funcs.append(func2.remote(y=2))
results = ray.get(funcs)
print(results)
view rawray_example_lists.py hosted with ❤ by GitHub
Source: Konstantinos Patronas
Via: betterprogramming
Tags: Python Functions in Parallel Using Ray API
ShareTweet

Get real time update about this post categories directly on your device, subscribe now.

Unsubscribe

Search

No Result
View All Result

Recent News

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023

About What We Do

itechnewsonline.com

We bring you the best Premium Tech News.

Recent News With Image

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025

Recent News

  • Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa July 29, 2025
  • French Telco Orange Hit by Cyber-Attack July 29, 2025
  • ATC Ghana supports Girls-In-ICT Program April 25, 2023
  • Vice President Dr. Bawumia inaugurates ICT Hub April 2, 2023
  • Home
  • InfoSec
  • Opinion
  • Africa Tech
  • Data Storage

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In

Add New Playlist

No Result
View All Result
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
Go to mobile version