You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I needed to decouple sender and receiver and initialized the channel with max_buffer_size>0. Lost elements are accepted when buffer is full, but sendchannel.send_nowait throws
TypeError: object NoneType can't be used in 'await' expression
I had expected that the channel could be filled up to max_buffer_size.
For demonstration I made minor changes to the threaded channel example:
importtriodefthread_fn(receive_from_trio, send_to_trio):
whileTrue:
# Since we're in a thread, we can't call methods on Trio# objects directly -- so we use trio.from_thread to call them.try:
request=trio.from_thread.run(receive_from_trio.receive)
response=request+1trio.from_thread.run(send_to_trio.send_nowait, response)
excepttrio.WouldBlock:
print('would block')
excepttrio.EndOfChannel:
trio.from_thread.run(send_to_trio.aclose)
returnasyncdefmain():
send_to_thread, receive_from_trio=trio.open_memory_channel(1)
send_to_trio, receive_from_thread=trio.open_memory_channel(1)
asyncwithtrio.open_nursery() asnursery:
# In a background thread, run:# thread_fn(portal, receive_from_trio, send_to_trio)nursery.start_soon(
trio.to_thread.run_sync, thread_fn, receive_from_trio, send_to_trio
)
# prints "1"awaitsend_to_thread.send(0)
# simulate a slow starting receiverawaittrio.sleep(2)
print(awaitreceive_from_thread.receive())
# prints "2"awaitsend_to_thread.send(1)
print(awaitreceive_from_thread.receive())
# When we close the channel, it signals the thread to exit.awaitsend_to_thread.aclose()
# When we exit the nursery, it waits for the background thread to# exit.trio.run(main)
The text was updated successfully, but these errors were encountered:
Ah, that's a bit tricky. The issue is that send is an async function, but send_nowait is a sync function, so you call them differently.
Normally, the difference is that you use await on async functions, and don't on sync functions, and that's the underlying issue here.
But, it's a bit obfuscated, because here you're not calling the function directly – instead you're passing it to trio.from_thread, and it's doing the actual calling.
So the issue here is that trio.from_thread.run is used for async functions. If you want to call a sync function from a thread, you have to switch to using trio.from_thread.run_sync instead.
So that should solve your immediate problem. In the longer run – maybe we can give a more helpful error message here?
I expected something in that area, but thought that trio.from_thread would handle this internally. A helpful error message should cover this case but in the meantime a note in the docs could be sufficient.
Thanks.
njsmith
changed the title
sendchannel.send_nowait throws TypeError: object NoneType can't be used in 'await' expression
In trio.to_thread.run, give a better error message if passed a sync function
Nov 4, 2019
I needed to decouple sender and receiver and initialized the channel with
max_buffer_size>0
. Lost elements are accepted when buffer is full, butsendchannel.send_nowait
throwsI had expected that the channel could be filled up to max_buffer_size.
For demonstration I made minor changes to the threaded channel example:
The text was updated successfully, but these errors were encountered: