I am experimenting with asyncio
(in python3.8). First of all, a small test of how to use asyncio.Queue
; simply put a data in the queue and get it again.
import asyncio
class QServer:
def _getQueue( self, queueName ):
queue = self._queues.get( queueName )
if queue is None:
queue = asyncio.Queue( )
self._queues[queueName] = asyncio.Queue( )
return queue
def __init__( self ):
self._queues = { }
async def put( self, queueName, data ):
queue = self._getQueue( queueName )
await queue.put( data )
async def get( self, queueName ):
queue = self._getQueue( queueName )
return await queue.get( )
async def main( ):
QueueName = 'TestQueue'
TestData = 'TestData'
queue = QServer( )
print( 'put' )
await queue.put( QueueName, TestData )
internal = queue._getQueue( QueueName )
print( internal.qsize( ) )
print( 'type' )
assert( type( queue._queues[ QueueName ] ) == type( asyncio.Queue( ) ) )
print( 'get' )
assert( await queue.get( QueueName ) == TestData )
print( 'End' )
asyncio.run( main( ) )
The output I expected is:
put
1
type
get
End
However, I am getting this other one:
put
0
type
get
And there the program stays on hold indefinitely, until I press Ctrl+Cand finish it.
To my uninitiated eyes, it looks like it put( )
's not putting anything in the queue. And I have a suspicion that I am not correctly understanding how coroutines work .
- What am I doing wrong ?
- How do I solve it ?