tmontes / ppytty

WIP: Present Python on a TTY
MIT License
1 stars 0 forks source link

Make all (most) traps non-blocking. #46

Open tmontes opened 6 years ago

tmontes commented 6 years ago

Sub-issue of #36.

Objective:

With this, instead of:

def task():
    yield ('sleep', 5)    # Blocks for 5 seconds

...the following code should be used:

def task():
    request_id = yield ('sleep', 5)            # Does not block
    sender, message = yield ('message-wait',)  # This blocks until we get a message
    assert sender is None
    msg_request_id, sleep_result = message
    assert msg_request_id == request_id
    # discard sleep_result, not useful

Pros:

Possible message-wait Improvement:

tmontes commented 6 years ago

Idea for message matching:

This provides good support for async traps as in:

def task():
     request_id = yield ('sleep', 5)
    _sender, _key, _discard = yield ('message-wait', None, request_id)
    # _sender is guaranteed to be None, per the sender passed to message-wait: meaning the kernel
    # _key is guaranteed to be request id, per the key passed to message-wait: meaning the sleep trap
    # _discard is whatever the sleep trap returns which is actually None.

...which, given none of the returning values are actually useful, could be written as:

def task():
    request_id = yield ('sleep', 5)
    yield ('message-wait', None, request_id)
tmontes commented 6 years ago

This issue stems from the potential need to wait/block on multiple things:

With the current task-wait and message-wait traps that is already possible, even though it needs to be done at a higher level: current solution depend on a task spawning a child task for each of the "things" it wants to wait/block on. Then either task-wait or message-wait will handle any of the child tasks results or messages.

Given this, I'll be deferring this interesting but maybe non-fundamental issue into TBD.