Giter Site home page Giter Site logo

Comments (2)

benkay86 avatar benkay86 commented on July 1, 2024

Note, this appears to actually be a bug (or a footgun) in Python. The polars-free code below deadlocks if you comment out the nonlocal count line. The reproducible example code above works (no deadlock) if you add nonlocal df to the top of the worker coroutine. Please feel free to close this issue.

import asyncio

async def arange(count):
    for i in range(count):
        yield(i)
        await asyncio.sleep(0)

async def main():
    # Initialize number.
    count = 0

    # Define a queue of data to process.
    limit = 3
    queue = asyncio.Queue(limit)

    # Define a worker task to do the processing.
    async def worker():
        nonlocal count
        while True:
            # Get the next item to process from the queue.
            item = await queue.get()
            # Increment the count.
            count = count + 1
            # Tell the queue we are done with this item.
            queue.task_done()

    # Launch some asynchronous worker tasks to process the queue.
    tasks = set()
    for _ in range(limit):
        tasks.add(asyncio.create_task(worker()))

    # Add some items to the queue.
    async for item in arange(100):
        # Put each item in the queue.
        await queue.put(item)
    
    # Wait until the queue is processed.
    await queue.join()
    # Then cancel the worker tasks.
    for task in tasks:
        task.cancel()
    for task in tasks:
        try:
            await task
        except asyncio.CancelledError:
            pass
    
    return count

if __name__ == '__main__':
    count = asyncio.run(main())
    print(count)

from polars.

stinodego avatar stinodego commented on July 1, 2024

In that case I'll close this issue. Thanks for looking into it.

from polars.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.