title: Async TCP Server Programming with Python And Postgresql
body: <p>I have some embedded C programming experience but new with Python.</p>
  <p>I designed a sensor circuit. The circuit will connect a TCP server via GPRS and will send some sensor data. I will have 1000-2000 device and those ones will stay connected to TCP server 7/24 and each of them will send approximately 100-150 byte data to TCP server in every 1 minute. The TCP server will parse( function didn't applied in the code below ) those data and will write to a Postgresql database table.</p>
  <p>I should write a TCP server which can scale that traffic. I will have 1000-2000 device, because of this i don't want to use thread approach for my TCP server. So i decided to write with async approach.</p>
  <p>I did some research for that and i tried to make the code below. But i don't know how to test this code without having 1000 device on the site and don't know this works asynchronously, without blocking the cpu.</p>
  <p>If this works as expected( asynchronously ) can this approach scale 1000-2000 device when every device will send 100-150 byte data in every 1 minute with a 1 core, 2GB ram virtual private server?</p>
  <p>And basically i can send a setting to the devices, about which device should send its data in which second. By doing this, in each second i will have approximately 40 data structure ( lets say i have 2000 device every minute i will have 2000 data structure 2000/60=33.3 approx. 40 data structure per sec )</p>
  <p>Thanks for your answer and best regards.</p>
  <pre><code>import asyncio
  import asyncpg
  #async olup olmadığı bilinmiyor.
  
  class EchoServerProtocol(asyncio.Protocol):
      def connection_made(self, transport):
          peername = transport.get_extra_info('peername')
          print('Connection from {}'.format(peername))
          self.transport = transport   
  
          
      def data_received(self, data):
          message = data.decode()
          print('Data received: {!r}'.format(message))
          
          database_handle()
          
          print('Send: {!r}'.format(message))
          self.transport.write(data)
  
          print('Close the client socket')
          self.transport.close()
  
      def connection_lost(self, transport):
          peername = self.transport.get_extra_info('peername')
          print('{} is disconnected from server'.format(peername))
          return super().connection_lost(transport)
  
  async def database_handle(data):
      async with poolsql.acquire() as connection:
      # Open a transaction.
          async with connection.transaction():
          # Run the query passing the request argument.
              result = await connection.execute('''INSERT INTO deneme(data) VALUES($1)''',data)
              print(result)
      
  async def main():
      # Get a reference to the event loop as we plan to use
      # low-level APIs.
      global poolsql
      poolsql=await asyncpg.create_pool('postgres://username:password@localhost:5432/test')
      
      loop = asyncio.get_running_loop()
      server = await loop.create_server(
          lambda: EchoServerProtocol(),
          '127.0.0.1', 8544, backlog=65000)
      
      async with server:
          await server.serve_forever()
  
  
  asyncio.run(main())
  </code></pre>