Best Python code snippet using autotest_python
uvm_exports.py
Source:uvm_exports.py  
1#2#------------------------------------------------------------------------------3#   Copyright 2007-2011 Mentor Graphics Corporation4#   Copyright 2007-2010 Cadence Design Systems, Inc.5#   Copyright 2010 Synopsys, Inc.6#   Copyright 2019-2020 Tuomas Poikela (tpoikela)7#   All Rights Reserved Worldwide8#9#   Licensed under the Apache License, Version 2.0 (the10#   "License"); you may not use this file except in11#   compliance with the License.  You may obtain a copy of12#   the License at13#14#       http://www.apache.org/licenses/LICENSE-2.015#16#   Unless required by applicable law or agreed to in17#   writing, software distributed under the License is18#   distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR19#   CONDITIONS OF ANY KIND, either express or implied.  See20#   the License for the specific language governing21#   permissions and limitations under the License.22#------------------------------------------------------------------------------23"""24Title: TLM Export Classes25The following classes define the TLM export classes.26CLASS: UVM*Export27The unidirectional UVM*Export is a port that ~forwards~ or ~promotes~28an interface implementation from a child component to its parent.29An export can be connected to any compatible child export or imp port.30It must ultimately be connected to at least one implementation31of its associated interface.32The interface type represented by the asterisk is any of the following33  blocking_put34  nonblocking_put35  put36  blocking_get37  nonblocking_get38  get39  blocking_peek40  nonblocking_peek41  peek42  blocking_get_peek43  nonblocking_get_peek44  get_peek45Exports are connected to interface implementations directly via46UVM*Imp ports or indirectly via other UVM*Export exports.47Function: __init__48The ~name~ and ~parent~ are the standard `UVMComponent` constructor arguments.49The ~min_size~ and ~max_size~ specify the minimum and maximum number of50interfaces that must have been supplied to this port by the end of elaboration.51.. code-block:: python52    def __init__(self, name, parent, min_size=1, max_size=1):53"""54from ..base.uvm_port_base import UVMPortBase55from .uvm_tlm_imps import (UVM_BLOCKING_GET_IMP, UVM_BLOCKING_GET_PEEK_IMP, UVM_BLOCKING_PEEK_IMP,56   UVM_BLOCKING_PUT_IMP, UVM_BLOCKING_TRANSPORT_IMP, UVM_EXPORT_COMMON,57   UVM_GET_IMP, UVM_GET_PEEK_IMP, UVM_NONBLOCKING_GET_IMP,58   UVM_NONBLOCKING_GET_PEEK_IMP, UVM_NONBLOCKING_PEEK_IMP,59   UVM_NONBLOCKING_PUT_IMP, UVM_NONBLOCKING_TRANSPORT_IMP, UVM_PEEK_IMP,60   UVM_PUT_IMP, UVM_TRANSPORT_IMP)61from ..macros.uvm_tlm_defines import (UVM_TLM_BLOCKING_GET_MASK, UVM_TLM_BLOCKING_GET_PEEK_MASK,62  UVM_TLM_BLOCKING_MASTER_MASK, UVM_TLM_BLOCKING_PEEK_MASK,63  UVM_TLM_BLOCKING_PUT_MASK, UVM_TLM_BLOCKING_SLAVE_MASK,64  UVM_TLM_BLOCKING_TRANSPORT_MASK, UVM_TLM_GET_MASK,65  UVM_TLM_GET_PEEK_MASK, UVM_TLM_MASTER_MASK,66  UVM_TLM_NONBLOCKING_GET_MASK,67  UVM_TLM_NONBLOCKING_GET_PEEK_MASK,68  UVM_TLM_NONBLOCKING_MASTER_MASK,69  UVM_TLM_NONBLOCKING_PEEK_MASK, UVM_TLM_NONBLOCKING_PUT_MASK,70  UVM_TLM_NONBLOCKING_SLAVE_MASK,71  UVM_TLM_NONBLOCKING_TRANSPORT_MASK, UVM_TLM_PEEK_MASK,72  UVM_TLM_PUT_MASK, UVM_TLM_SLAVE_MASK, UVM_TLM_TRANSPORT_MASK)73#class uvm_blocking_put_export #(type T=int)74#  extends uvm_port_base #(uvm_tlm_if_base #(T,T));75#  `UVM_EXPORT_COMMON(`UVM_TLM_BLOCKING_PUT_MASK,"uvm_blocking_put_export")76#  `UVM_BLOCKING_PUT_IMP ('m_if', T, t)77class UVMBlockingPutExport(UVMPortBase):78    pass79UVMBlockingPutExport = UVM_EXPORT_COMMON(UVMBlockingPutExport,  # type: ignore80    UVM_TLM_BLOCKING_PUT_MASK,"uvm_blocking_put_export")81UVM_BLOCKING_PUT_IMP('m_if', UVMBlockingPutExport)82#class uvm_nonblocking_put_export #(type T=int)83#  extends uvm_port_base #(uvm_tlm_if_base #(T,T));84#  `UVM_EXPORT_COMMON(`UVM_TLM_NONBLOCKING_PUT_MASK,"uvm_nonblocking_put_export")85#  `UVM_NONBLOCKING_PUT_IMP ('m_if', T, t)86class uvm_nonblocking_put_export(UVMPortBase):87    pass88uvm_nonblocking_put_export = UVM_EXPORT_COMMON(uvm_nonblocking_put_export,  # type: ignore89    UVM_TLM_NONBLOCKING_PUT_MASK,"uvm_nonblocking_put_export")90UVM_NONBLOCKING_PUT_IMP('m_if', uvm_nonblocking_put_export)91UVMNonBlockingPutExport = uvm_nonblocking_put_export92#class uvm_put_export #(type T=int)93#  extends uvm_port_base #(uvm_tlm_if_base #(T,T));94#  `UVM_EXPORT_COMMON(`UVM_TLM_PUT_MASK,"uvm_put_export")95#  `UVM_PUT_IMP ('m_if', T, t)96class uvm_put_export(UVMPortBase):97    pass98uvm_put_export = UVM_EXPORT_COMMON(uvm_put_export, UVM_TLM_PUT_MASK,  # type: ignore99    "uvm_put_export")100UVM_PUT_IMP('m_if', uvm_put_export)101UVMPutExport = uvm_put_export102#class uvm_blocking_get_export #(type T=int)103#  extends uvm_port_base #(uvm_tlm_if_base #(T,T));104#  `UVM_EXPORT_COMMON(`UVM_TLM_BLOCKING_GET_MASK,"uvm_blocking_get_export")105#  `UVM_BLOCKING_GET_IMP ('m_if', T, t)106class uvm_blocking_get_export(UVMPortBase):107    pass108uvm_blocking_get_export = UVM_EXPORT_COMMON(uvm_blocking_get_export,  # type: ignore109    UVM_TLM_BLOCKING_GET_MASK,"uvm_blocking_get_export")110UVM_BLOCKING_GET_IMP('m_if', uvm_blocking_get_export)111UVMBlockingGetExport = uvm_blocking_get_export112#class uvm_nonblocking_get_export #(type T=int)113#  extends uvm_port_base #(uvm_tlm_if_base #(T,T));114#  `UVM_EXPORT_COMMON(`UVM_TLM_NONBLOCKING_GET_MASK,"uvm_nonblocking_get_export")115#  `UVM_NONBLOCKING_GET_IMP ('m_if', T, t)116class uvm_nonblocking_get_export(UVMPortBase):117    pass118uvm_nonblocking_get_export = UVM_EXPORT_COMMON(uvm_nonblocking_get_export,  # type: ignore119    UVM_TLM_NONBLOCKING_GET_MASK,"uvm_nonblocking_get_export")120UVM_NONBLOCKING_GET_IMP('m_if', uvm_nonblocking_get_export)121UVMNonBlockingGetExport = uvm_nonblocking_get_export122#class uvm_get_export #(type T=int)123#  extends uvm_port_base #(uvm_tlm_if_base #(T,T));124#  `UVM_EXPORT_COMMON(`UVM_TLM_GET_MASK,"uvm_get_export")125#  `UVM_GET_IMP ('m_if', T, t)126class uvm_get_export(UVMPortBase):127    pass128uvm_get_export = UVM_EXPORT_COMMON(uvm_get_export, UVM_TLM_GET_MASK,  # type: ignore129    "uvm_get_export")130UVM_GET_IMP('m_if', uvm_get_export)131UVMGetExport = uvm_get_export132#class uvm_blocking_peek_export #(type T=int)133#  extends uvm_port_base #(uvm_tlm_if_base #(T,T));134#  `UVM_EXPORT_COMMON(`UVM_TLM_BLOCKING_PEEK_MASK,"uvm_blocking_peek_export")135#  `UVM_BLOCKING_PEEK_IMP ('m_if', T, t)136class uvm_blocking_peek_export(UVMPortBase):137    pass138uvm_blocking_peek_export = UVM_EXPORT_COMMON(uvm_blocking_peek_export,  # type: ignore139    UVM_TLM_BLOCKING_PEEK_MASK,"uvm_blocking_peek_export")140UVM_BLOCKING_PEEK_IMP('m_if', uvm_blocking_peek_export)141UVMBlockingPeekExport = uvm_blocking_peek_export142#class uvm_nonblocking_peek_export #(type T=int)143#  extends uvm_port_base #(uvm_tlm_if_base #(T,T));144#  `UVM_EXPORT_COMMON(`UVM_TLM_NONBLOCKING_PEEK_MASK,"uvm_nonblocking_peek_export")145#  `UVM_NONBLOCKING_PEEK_IMP ('m_if', T, t)146class uvm_nonblocking_peek_export(UVMPortBase):147    pass148uvm_nonblocking_peek_export = UVM_EXPORT_COMMON(uvm_nonblocking_peek_export,  # type: ignore149    UVM_TLM_NONBLOCKING_PEEK_MASK,"uvm_nonblocking_peek_export")150UVM_NONBLOCKING_PEEK_IMP('m_if', uvm_nonblocking_peek_export)151UVMNonBlockingPeekExport = uvm_nonblocking_peek_export152#class uvm_peek_export #(type T=int)153#  extends uvm_port_base #(uvm_tlm_if_base #(T,T));154#  `UVM_EXPORT_COMMON(`UVM_TLM_PEEK_MASK,"uvm_peek_export")155#  `UVM_PEEK_IMP ('m_if', T, t)156class uvm_peek_export(UVMPortBase):157    pass158uvm_peek_export = UVM_EXPORT_COMMON(uvm_peek_export, UVM_TLM_PEEK_MASK,  # type: ignore159    "uvm_peek_export")160UVM_PEEK_IMP ('m_if', uvm_peek_export)161UVMPeekExport = uvm_peek_export162#class uvm_blocking_get_peek_export #(type T=int)163#  extends uvm_port_base #(uvm_tlm_if_base #(T,T));164#  `UVM_EXPORT_COMMON(`UVM_TLM_BLOCKING_GET_PEEK_MASK,"uvm_blocking_get_peek_export")165#  `UVM_BLOCKING_GET_PEEK_IMP ('m_if', T, t)166class uvm_blocking_get_peek_export(UVMPortBase):167    pass168uvm_blocking_get_peek_export = UVM_EXPORT_COMMON(uvm_blocking_get_peek_export, # type: ignore169    UVM_TLM_BLOCKING_GET_PEEK_MASK,"uvm_blocking_get_peek_export")170UVM_BLOCKING_GET_PEEK_IMP('m_if', uvm_blocking_get_peek_export)171UVMBlockingGetPeekExport = uvm_blocking_get_peek_export172#class uvm_nonblocking_get_peek_export #(type T=int)173#  extends uvm_port_base #(uvm_tlm_if_base #(T,T));174#  `UVM_EXPORT_COMMON(`UVM_TLM_NONBLOCKING_GET_PEEK_MASK,"uvm_nonblocking_get_peek_export")175#  `UVM_NONBLOCKING_GET_PEEK_IMP ('m_if', T, t)176class uvm_nonblocking_get_peek_export(UVMPortBase):177    pass178uvm_nonblocking_get_peek_export = UVM_EXPORT_COMMON(uvm_nonblocking_get_peek_export,  # type: ignore179    UVM_TLM_NONBLOCKING_GET_PEEK_MASK,"uvm_nonblocking_get_peek_export")180UVM_NONBLOCKING_GET_PEEK_IMP ('m_if', uvm_nonblocking_get_peek_export)181UVMNonBlockingGetPeekExport = uvm_nonblocking_get_peek_export182#class uvm_get_peek_export #(type T=int)183#  extends uvm_port_base #(uvm_tlm_if_base #(T,T));184#  `UVM_EXPORT_COMMON(`UVM_TLM_GET_PEEK_MASK,"uvm_get_peek_export")185#  `UVM_GET_PEEK_IMP ('m_if', T, t)186class uvm_get_peek_export(UVMPortBase):187    pass188uvm_get_peek_export = UVM_EXPORT_COMMON(uvm_get_peek_export,  # type: ignore189    UVM_TLM_GET_PEEK_MASK,"uvm_get_peek_export")190UVM_GET_PEEK_IMP('m_if', uvm_get_peek_export)191UVMGetPeekExport = uvm_get_peek_export192#------------------------------------------------------------------------------193#194# CLASS: uvm_*_export #(REQ,RSP)195#196# The bidirectional uvm_*_export is a port that ~forwards~ or ~promotes~197# an interface implementation from a child component to its parent.198# An export can be connected to any compatible child export or imp port.199# It must ultimately be connected to at least one implementation200# of its associated interface.201#202# The interface type represented by the asterisk is any of the following203#204#|  blocking_transport205#|  nonblocking_transport206#|  transport207#|208#|  blocking_master209#|  nonblocking_master210#|  master211#|212#|  blocking_slave213#|  nonblocking_slave214#|  slave215#216# Type parameters217#218# REQ - The type of request transaction to be communicated by the export219#220# RSP - The type of response transaction to be communicated by the export221#222# Exports are connected to interface implementations directly via223# <uvm_*_imp #(REQ, RSP, IMP, REQ_IMP, RSP_IMP)> ports or indirectly via other224# <uvm_*_export #(REQ,RSP)> exports.225#226#------------------------------------------------------------------------------227#228# Function: new229#230# The ~name~ and ~parent~ are the standard <uvm_component> constructor arguments.231# The ~min_size~ and ~max_size~ specify the minimum and maximum number of232# interfaces that must have been supplied to this port by the end of elaboration.233#234#|  function new (string name,235#|                uvm_component parent,236#|                int min_size=1,237#|                int max_size=1)238#class uvm_blocking_master_export #(type REQ=int, type RSP=REQ)239#  extends uvm_port_base #(uvm_tlm_if_base #(REQ, RSP));240#  `UVM_EXPORT_COMMON(`UVM_TLM_BLOCKING_MASTER_MASK,"uvm_blocking_master_export")241#  `UVM_BLOCKING_PUT_IMP ('m_if', REQ, t)242#  `UVM_BLOCKING_GET_PEEK_IMP ('m_if', RSP, t)243class uvm_blocking_master_export:244    pass245uvm_blocking_master_export = UVM_EXPORT_COMMON(uvm_blocking_master_export,  # type: ignore246    UVM_TLM_BLOCKING_MASTER_MASK,"uvm_blocking_master_export")247UVM_BLOCKING_PUT_IMP('m_if', uvm_blocking_master_export)248UVM_BLOCKING_GET_PEEK_IMP('m_if', uvm_blocking_master_export)249UVMBlockingMasterExport = uvm_blocking_master_export250#class uvm_nonblocking_master_export #(type REQ=int, type RSP=REQ)251#  extends uvm_port_base #(uvm_tlm_if_base #(REQ, RSP));252#  `UVM_EXPORT_COMMON(`UVM_TLM_NONBLOCKING_MASTER_MASK,"uvm_nonblocking_master_export")253#  `UVM_NONBLOCKING_PUT_IMP ('m_if', REQ, t)254#  `UVM_NONBLOCKING_GET_PEEK_IMP ('m_if', RSP, t)255class uvm_nonblocking_master_export:256    pass257uvm_nonblocking_master_export = UVM_EXPORT_COMMON(uvm_nonblocking_master_export,  # type: ignore258    UVM_TLM_NONBLOCKING_MASTER_MASK, "uvm_nonblocking_master_export")259UVM_NONBLOCKING_PUT_IMP('m_if', uvm_nonblocking_master_export)260UVM_NONBLOCKING_GET_PEEK_IMP('m_if', uvm_nonblocking_master_export)261UVMNonBlockingMasterExport = uvm_nonblocking_master_export262#class uvm_master_export #(type REQ=int, type RSP=REQ)263#  extends uvm_port_base #(uvm_tlm_if_base #(REQ, RSP));264#  `UVM_EXPORT_COMMON(`UVM_TLM_MASTER_MASK,"uvm_master_export")265#  `UVM_PUT_IMP ('m_if', REQ, t)266#  `UVM_GET_PEEK_IMP ('m_if', RSP, t)267class uvm_master_export:268    pass269uvm_master_export = UVM_EXPORT_COMMON(uvm_master_export,  # type: ignore270    UVM_TLM_MASTER_MASK, "uvm_master_port")271UVM_PUT_IMP('m_if', uvm_master_export)272UVM_GET_PEEK_IMP('m_if', uvm_master_export)273UVMMasterExport = uvm_master_export274#class uvm_blocking_slave_export #(type REQ=int, type RSP=REQ)275#  extends uvm_port_base #(uvm_tlm_if_base #(RSP, REQ));276#  `UVM_EXPORT_COMMON(`UVM_TLM_BLOCKING_SLAVE_MASK,"uvm_blocking_slave_export")277#  `UVM_BLOCKING_PUT_IMP ('m_if', RSP, t)278#  `UVM_BLOCKING_GET_PEEK_IMP ('m_if', REQ, t)279class uvm_blocking_slave_export:  #(type REQ=int, type RSP=REQ)280    pass281uvm_blocking_slave_export = UVM_EXPORT_COMMON(uvm_blocking_slave_export,  # type: ignore282    UVM_TLM_BLOCKING_SLAVE_MASK, "uvm_blocking_slave_export")283UVM_BLOCKING_PUT_IMP('m_if', uvm_blocking_slave_export)284UVM_BLOCKING_GET_PEEK_IMP('m_if', uvm_blocking_slave_export)285#class uvm_nonblocking_slave_export #(type REQ=int, type RSP=REQ)286#  extends uvm_port_base #(uvm_tlm_if_base #(RSP, REQ));287#  `UVM_EXPORT_COMMON(`UVM_TLM_NONBLOCKING_SLAVE_MASK,"uvm_nonblocking_slave_export")288#  `UVM_NONBLOCKING_PUT_IMP ('m_if', RSP, t)289#  `UVM_NONBLOCKING_GET_PEEK_IMP ('m_if', REQ, t)290class uvm_nonblocking_slave_export:  #(type REQ=int, type RSP=REQ)291    pass292uvm_nonblocking_slave_export = UVM_EXPORT_COMMON(uvm_nonblocking_slave_export, # type: ignore293    UVM_TLM_NONBLOCKING_SLAVE_MASK,"uvm_nonblocking_slave_export")294UVM_NONBLOCKING_PUT_IMP('m_if', uvm_nonblocking_slave_export)295UVM_NONBLOCKING_GET_PEEK_IMP('m_if', uvm_nonblocking_slave_export)296UVMNonBlockingSlaveExport = uvm_nonblocking_slave_export297#class uvm_slave_export #(type REQ=int, type RSP=REQ)298#  extends uvm_port_base #(uvm_tlm_if_base #(RSP, REQ));299#  `UVM_EXPORT_COMMON(`UVM_TLM_SLAVE_MASK,"uvm_slave_export")300#  `UVM_PUT_IMP ('m_if', RSP, t)301#  `UVM_GET_PEEK_IMP ('m_if', REQ, t)302class uvm_slave_export:  # (type REQ=int, type RSP=REQ)303    pass304uvm_slave_export = UVM_EXPORT_COMMON(uvm_slave_export,  # type: ignore305    UVM_TLM_SLAVE_MASK, "uvm_slave_export")306UVM_PUT_IMP('m_if', uvm_slave_export)307UVM_GET_PEEK_IMP('m_if', uvm_slave_export)308UVMSlaveExport = uvm_slave_export309#class uvm_blocking_transport_export #(type REQ=int, type RSP=REQ)310#  extends uvm_port_base #(uvm_tlm_if_base #(REQ, RSP));311#  `UVM_EXPORT_COMMON(`UVM_TLM_BLOCKING_TRANSPORT_MASK,"uvm_blocking_transport_export")312#  `UVM_BLOCKING_TRANSPORT_IMP ('m_if', REQ, RSP, req, rsp)313class uvm_blocking_transport_export:  #(type REQ=int, type RSP=REQ)314    pass315uvm_blocking_transport_export = UVM_EXPORT_COMMON(uvm_blocking_transport_export,  # type: ignore316    UVM_TLM_BLOCKING_TRANSPORT_MASK, "uvm_blocking_transport_export")317UVM_BLOCKING_TRANSPORT_IMP('m_if', uvm_blocking_transport_export)318UVMBlockingTransportExport = uvm_blocking_transport_export319#class uvm_nonblocking_transport_export #(type REQ=int, type RSP=REQ)320#  extends uvm_port_base #(uvm_tlm_if_base #(REQ, RSP));321#  `UVM_EXPORT_COMMON(`UVM_TLM_NONBLOCKING_TRANSPORT_MASK,"uvm_nonblocking_transport_export")322#  `UVM_NONBLOCKING_TRANSPORT_IMP ('m_if', REQ, RSP, req, rsp)323class uvm_nonblocking_transport_export:  #(type REQ=int, type RSP=REQ)324    pass325uvm_nonblocking_transport_export = UVM_EXPORT_COMMON(uvm_nonblocking_transport_export,  # type: ignore326    UVM_TLM_NONBLOCKING_TRANSPORT_MASK,"uvm_nonblocking_transport_export")327UVM_NONBLOCKING_TRANSPORT_IMP('m_if', uvm_nonblocking_transport_export)328UVMNonBlockingTransportExport = uvm_nonblocking_transport_export329#class uvm_transport_export #(type REQ=int, type RSP=REQ)330#  extends uvm_port_base #(uvm_tlm_if_base #(REQ, RSP));331#  `UVM_EXPORT_COMMON(`UVM_TLM_TRANSPORT_MASK,"uvm_transport_export")332#  `UVM_TRANSPORT_IMP ('m_if', REQ, RSP, req, rsp)333class uvm_transport_export:  #(type REQ=int, type RSP=REQ)334    pass335uvm_transport_export = UVM_EXPORT_COMMON(uvm_transport_export,  # type: ignore336    UVM_TLM_TRANSPORT_MASK,"uvm_transport_export")337UVM_TRANSPORT_IMP ('m_if', uvm_transport_export)...iter.py
Source:iter.py  
1import time2import types3from torch.utils.data import IterDataPipe, communication4DEFAULT_NON_BLOCKING_SLEEP = 0.0015def default_not_available_hook():6    time.sleep(DEFAULT_NON_BLOCKING_SLEEP)7class NotAvailable(Exception):8    pass9class InvalidStateResetRequired(Exception):10    """11        Returned by DataPipe when it is expecting to get reset request,12        for example RouterDataPipe expecting all workers to request reset'13    """14    pass15class NonBlocking(IterDataPipe):16    not_available_hook = default_not_available_hook17    def __iter__(self):18        self.reset_iterator()19        return self20    def __next__(self):21        while True:22            try:23                return self.nonblocking_next()24            except StopIteration:25                raise StopIteration26            except NotAvailable:27                if NonBlocking.not_available_hook is not None:28                    NonBlocking.not_available_hook()29    def nonblocking_next(self):30        raise NotImplementedError(31            "nonblocking_next is not implemented for %s" % self.__class__)32    def reset_iterator(self):33        raise NotImplementedError(34            "reset_iterator is not implemented for %s" % self.__class__)35    @staticmethod36    def register_not_available_hook(hook_function):37        NonBlocking.not_available_hook = hook_function38def EnsureNonBlockingDataPipe(validated_datapipe):39    if not isinstance(validated_datapipe, IterDataPipe):40        raise Exception('Not Iterable DataPipe ' +41                        str(validated_datapipe.__class__))42    if isinstance(validated_datapipe, NonBlocking):43        return validated_datapipe44    if not hasattr(validated_datapipe, '_as_iterator'):45        validated_datapipe._as_iterator = None  # type: ignore[attr-defined]46    if not hasattr(validated_datapipe, 'nonblocking_next'):47        def nonblocking_next(self):48            if self._as_iterator is None:49                self._as_iterator = iter(self)50            return next(self._as_iterator)51        validated_datapipe.nonblocking_next = types.MethodType(  # type: ignore[attr-defined]52            nonblocking_next, validated_datapipe)53    if not hasattr(validated_datapipe, 'reset_iterator'):54        def reset_iterator(self):55            self._as_iterator = None56        validated_datapipe.reset_iterator = types.MethodType(  # type: ignore[attr-defined]57            reset_iterator, validated_datapipe)58    return validated_datapipe59def DataPipeBehindQueues(source_datapipe, protocol, full_stop=False, blocking_request_get=False):60    """61        Indefinitely iterates over req_queue and passing values from source_datapipe to res_queue62        If raise_stop is true, raises exception when StopIteration received from the source_datapipe63    """64    if not isinstance(protocol, communication.protocol.IterDataPipeQueueProtocolServer):65        raise Exception('Expecting IterDataPipeQueueProtocolServer, got', protocol)66    source_datapipe = EnsureNonBlockingDataPipe(source_datapipe)67    forever = True68    while forever:69        try:70            # Non-blocking call is Extremely slow here for python.mp, need to figureout good workaround71            request = protocol.get_new_request(block=blocking_request_get)72        except communication.protocol.EmptyQueue:73            yield True74            continue75        if isinstance(request, communication.messages.ResetIteratorRequest):76            source_datapipe.reset_iterator()77            protocol.response_reset()78        elif isinstance(request, communication.messages.TerminateRequest):79            forever = False80            protocol.response_terminate()81        elif isinstance(request, communication.messages.GetNextRequest):82            while forever:83                try:84                    value = source_datapipe.nonblocking_next()85                except NotAvailable:86                    yield True87                    continue88                except StopIteration:89                    protocol.response_stop()90                    if full_stop:91                        forever = False92                    else:93                        yield True94                    break95                except InvalidStateResetRequired:96                    protocol.response_invalid()97                    if full_stop:98                        forever = False99                    else:100                        yield True101                    break102                protocol.response_next(value)103                yield True  # Returns control104                break105        else:106            raise Exception('Unrecognized type of request received', request)107class QueueWrapper(NonBlocking):108    """109        Creates iter.DataPipe which reads data from the DataLoader.Queue110    """111    def __init__(self, protocol, response_wait_time=0.00001):112        if not isinstance(protocol, communication.protocol.IterDataPipeQueueProtocolClient):113            raise Exception('Got', protocol)114        self.protocol = protocol115        self.counter = 0116        self._stop_iteration = False117        self._response_wait_time = response_wait_time118    def reset_iterator(self):119        self._stop_iteration = False120        self.counter = 0121        self.protocol.request_reset()122        while True:123            try:124                self.protocol.get_response_reset()125                break126            except communication.protocol.EmptyQueue:127                if NonBlocking.not_available_hook is not None:128                    NonBlocking.not_available_hook()129    def nonblocking_next(self):130        if self._stop_iteration:131            raise Exception(132                '`next` or `nonblocking_next` called after receiving StopIteration')133        if self.protocol.can_take_request():134            self.protocol.request_next()135        try:136            response = self.protocol.get_response_next(block=True, timeout=self._response_wait_time)137        except communication.protocol.EmptyQueue:138            raise NotAvailable139        if isinstance(response, communication.messages.StopIterationResponse):140            self._stop_iteration = True141            raise StopIteration142        if isinstance(response, communication.messages.InvalidStateResponse):143            raise NotAvailable...nonblocking.py
Source:nonblocking.py  
1from multiprocessing.pool import ThreadPool2import threading3class Nonblocking:4    '''5    This is a base class that allows the inheriting class to mark6    its methods as nonblocking, by decorating them with @Nonblocking.method.7    If a method marked as nonblocking is called, it is run in a separate8    thread, and the calling thread continues execution while the method runs.9    In order to get the return value of this method, the client can call the10    <instance>.<method_name>.await_return() class method. This will cause the11    client thread to block until the nonblocking method has finished, at which12    point the return value is read.13    The immediate return value of a nonblocking method is a reference to the14    nonblocking method itself.15    This can be used to make nonblocking methods block like a normal method:16    E.g. return_value = myNonblockingMethod(...).await_return()17    If a method marked as nonblocking calls another nonblocking method (from18    its worker thread), a new worker thread will not be spawned, and the19    function call will happen in the usual blocking manner in the worker20    thread. This is to prevent an unbounded number of thread from being21    created.22    Optionally multiple worker threads can be created, but the default23    number of worker threads is one per Nonblocking class.24    If the client calls a nonblocking method while all (usually only one) the25    worker threads are in use, then the nonblocking method call will be added26    to a queue, and called once a worker thread is free.27    '''28    def __init__(self, threads=1):29        '''30        Use @threads to specify number of worker threads. You probably31        want to leave this at the default of 1 to prevent concurrency issues.32        '''33        self._thread_pool = ThreadPool(processes=threads)34        self._thread_ident = threading.get_ident()35    class method:36        '''37        Decorates class methods in order to make then nonblocking.38        '''39        def __init__(self, fn):40            self.fn = fn41            self.async_result = None42            self.parent = None43        def await_return(self):44            '''45            Block until nonblocking method has finished, then46            return its return value.47            '''48            if self.async_result:49                return self.async_result.get()50        def __get__(self, instance, owner):51            if instance is None:52                return self53            d = self54            # use a lambda to produce a bound method55            mfactory = lambda self, *args, **kw: d(self, *args, **kw)56            mfactory.__name__ = self.fn.__name__57            mfactory.await_return = self.await_return58            if not self.parent:59                # Make link to parent class60                self.parent = instance61            return mfactory.__get__(instance, owner)62        def __call__(self, *args, **kwargs):63            '''64            Intercept function calls and queue them in worker threads.65            '''66            thread_ident = threading.get_ident()67            if thread_ident != self.parent._thread_ident:68                return self.fn(*args, **kwargs)69            self.async_result = self.parent._thread_pool.apply_async(70                func=self.fn, args=args, kwds=kwargs)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
