python3套接字读取,避免使用True或最佳实践

时间:2017-08-08 01:06:31

标签: python-3.x network-programming ubuntu-14.04 python-3.5

我必须在python中编写一个简单的tcp转发器,它将永远运行。我将以1分钟的间隔获取数据。那么我必须在socket.read()中运行while true吗? 有没有更好的方法来避免所有那些不必要的cpu​​周期? 还有一件事,socket.read()在一个线程中。

2 个答案:

答案 0 :(得分:2)

  

我必须在true中运行socket.read()吗?有没有更好的方法来避免所有那些不必要的cpu​​循环?

这是阻止@extends('layouts.app') @section('content') <div class="container"> <div class="row"> <div class="col-md-8 col-md-offset-2"> <div class="panel panel-default"> <div class="panel-heading text-center">Add New Machine</div> <div class="panel-body"> <form class="form-horizontal" method="POST" action="{{ route('addMachine') }}"> {{ csrf_field() }} <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}"> <label for="name" class="col-md-4 control-label">Name</label> <div class="col-md-6"> <input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" required autofocus> @if ($errors->has('name')) <span class="help-block"> <strong>{{ $errors->first('name') }}</strong> </span> @endif </div> </div> <div class="form-group{{ $errors->has('part_id') ? ' has-error' : '' }}"> <label for="part_id" class="col-md-4 control-label">Part Id#</label> <div class="col-md-6"> <input class="typeahead form-control" autocomplete="off" type="text" id="part_id" name="part_id" value="{{ old('part_id') }}"> @if ($errors->has('part_id')) <span class="help-block"> <strong>{{ $errors->first('part_id') }}</strong> </span> @endif </div> </div> <div class="form-group"> <label for="department_id" class="col-md-4 control-label">Department</label> <div class="col-md-6"> <p style="margin-top: 7px;"> <select name="department_id" id="department_id" type="department_id"> <?php foreach ($departments as $department) { echo "<option value=".$department['id'].">".$department['name']."</option>"; } ?> </select> </div> </div> <div class="form-group"> <label for="type_id" class="col-md-4 control-label">Machine Type</label> <div class="col-md-6"> <p style="margin-top: 7px;"> <select name="type_id" id="type_id" type="type_id"> <?php foreach ($types as $type) { echo "<option value=".$type['id'].">".$type['name']."</option>"; } ?> </select> </div> </div> <div class="form-group"> <label for="production_status_id" class="col-md-4 control-label">Production Status</label> <div class="col-md-6"> <p style="margin-top: 7px;"> <select name="production_status_id" id="production_status_id" type="production_status_id"> <?php foreach ($productionStatuses as $productionStatus) { echo "<option value=".$productionStatus['id'].">".$productionStatus['name']."</option>"; } ?> </select> </div> </div> <div class="form-group"> <div class="col-md-6 col-md-offset-4"> <button type="submit" class="btn btn-primary"> Add Machine </button> </div> </div> </form> </div> </div> </div> </div> </div> @endsection @section('scripts') <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.2/bootstrap3-typeahead.min.js"></script> <script type="text/javascript"> var autocompletePartPath = "{{ route('autocompletePart') }}"; $('#part_id').typeahead( { source: function (query, process) { return $.get(autocompletePartPath, { query: query }, function (data) { return process(data); }); }, displayText: function (item) { return `${item.id} - ${item.name} ${item.revision}`; }, afterSelect: function (item) { $('#part_id').val(item.id); }, fitToElement: true }); </script> @endsection 。因此,您的进程(线程)在等待下一次网络通信时基本上处于休眠状态,而不是消耗CPU周期。

答案 1 :(得分:0)

在一天结束时,你仍然需要某种真实。通常的做法是在某个地方睡觉。

如果需要从多个套接字读取,则需要使用select函数。 https://docs.python.org/3/library/select.html。这是因为read()操作可能会阻塞。