PARL Logo
latest

Overview

  • Features
  • Abstractions
  • Parallelization

Installation

  • Installation

Tutorial

  • Getting Started
  • Model, Algorithm, Agent
  • Create Customized Algorithms
  • Save and Restore Parameters
  • Visualization Tool
  • CSV Logger

High-quality Implementations

  • Policy Gradient
  • DQN
  • DDPG
  • DDQN
  • OAC
  • A2C
  • TD3
  • QMIX
  • SAC
  • PPO
  • MADDPG

Parallel Training

  • Overview
  • Xparl Usage
  • Recommended Practice
  • Recommended Practice(no_wait mode)
  • GPU Cluster
  • How to Debug
  • File Distribution
  • Serialization Acceleration (Not Necessary)

APIs

  • parl.Model
  • parl.Algorithm
  • parl.Agent
  • parl.remote_class
  • parl.connect

Frequently Asked Questions

  • xparl questions
  • RL questions

EvoKit

  • Overview
  • minimal example
  • Example for Online Products
PARL
  • Docs »
  • Recommended Practice(no_wait mode)
  • Edit on GitHub

Recommended Practice(no_wait mode)¶

This tutorial shows how to use @parl.remote_class to implement parallel computation without multithreading.
In the previous tutorial, we implemented parallel computation through decorator and multithreading. PARL actually provides a more compact parallel computation mode without manually creating threads. By passing the argument wait=false to the decoratior, we can run tasks in parallel in a simpler way. The program will not be blocked while calling the functions of the decroated class. Instead, it will return a future_object immediately, and users can obtain the result in the future by calling future_object.get().
Recall that in the previous tutorial, we introduced a multithreading style parallel computation which looks as below.
import threading
import parl

@parl.remote_class
class A(object):
    def run(self):
        ans = 0
        for i in range(100000000):
            ans += i
threads = []
parl.connect("localhost:6006")
for _ in range(5):
    a = A()
    th = threading.Thread(target=a.run)
    th.start()
    threads.append(th)
for th in threads:
    th.join()
Now let’s look at how to implement it without threading.
import parl

@parl.remote_class(wait=False)
class A(object):
    def run(self):
        ans = 0
        for i in range(100000000):
            ans += i
        return ans

parl.connect("localhost:6006")
actors = [A() for _ in range(5)]
jobs = [actor.run() for actor in actors]
returns = [job.get() for job in jobs]

true_result = sum([i for i in range(100000000)])
for result in returns:
    assert result == true_result
two things to notice:
  1. We add wait=False in the remote decorator so that the program will not be blocked.
  2. After actors start running, calling job.get() will block the main program until the job is finished. The return of jog.get() is the same as calling the original function.
Next Previous

© Copyright 2021, nlp-ol@baidu.com Revision 545cc0e0.

Built with Sphinx using a theme provided by Read the Docs.