模块化开发可重用性:如何处理 While True 循环?

发布于 2024-09-28 01:54:42 字数 469 浏览 9 评论 0原文

我最近一直在使用 pybluez 模块来扫描附近的蓝牙设备。我现在想做的是扩展程序以查找附近的 WiFi 客户端设备。

WiFi 客户端扫描仪需要有一个 While True 循环来持续监控电波。如果我将其编写为一个直接的单文件程序,那就很容易了。

import ...

while True:
    client = scan()
    print client['mac']

然而,我想要的是使其成为一个模块。我希望以后能够重用它,并且可能的话,让其他人也使用它。我不明白的是如何处理循环。

import mymodule

scan()

假设第一个示例代码是“mymodule”,该程序将简单地将数据打印到标准输出。我希望能够在我的程序中使用这些数据,而不是让模块将其打印出来......

我应该如何对模块进行编码?

I've been playing around with the pybluez module recently to scan for nearby Bluetooth devices. What I want to do now is extend the program to also find nearby WiFi client devices.

The WiFi client scanner will have need to have a While True loop to continually monitor the airwaves. If I were to write this as a straight up, one file program, it would be easy.

import ...

while True:
    client = scan()
    print client['mac']

What I want, however, is to make this a module. I want to be able to reuse it later and, possible, have others use it too. What I can't figure out is how to handle the loop.

import mymodule

scan()

Assuming the first example code was 'mymodule', this program would simply print out the data to stdout. I would want to be able to use this data in my program instead of having the module print it out...

How should I code the module?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只为一人 2024-10-05 01:54:42

我认为最好的方法是让扫描仪在与主程序不同的线程上运行。该模块应该具有启动和停止扫描仪的方法,以及返回当前访问点列表的方法(使用锁进行同步)。请参阅线程模块

I think the best approach is going to be to have the scanner run on a separate thread from the main program. The module should have methods that start and stop the scanner, and another that returns the current access point list (using a lock to synchronize). See the threading module.

难理解 2024-10-05 01:54:42

一些非常简单的东西怎么样:

mymodule.py

import ...
def scanner():
    while True:
        client = scan()
        yield client['mac']

othermodule.py

import mymodule
for mac in mymodule.scanner():
    print mac

如果你想要比这更有用的东西,我还建议像@kindall那样使用后台线程。

How about something pretty straightforward like:

mymodule.py

import ...
def scanner():
    while True:
        client = scan()
        yield client['mac']

othermodule.py

import mymodule
for mac in mymodule.scanner():
    print mac

If you want something more useful than that, I'd also suggest a background thread as @kindall did.

紙鸢 2024-10-05 01:54:42

两个接口会很有用。

  1. scan() 本身,它返回找到的设备列表,这样我就可以调用它来获取可用蓝牙的即时快照。它可能需要 max_seconds_to_search 或 max_num_to_return 参数。
  2. 接受回调的“发现时通知”函数。例如(也许是拼写错误,我只是即兴写下这个)。

    def find_bluetooth(callback_func, time_to_search = 5.0):
      已经找到=[]
      开始时间 = time.clock()
      而1: 
        如果 time.clock()-start_time > 5.0:中断
        找到=扫描()
        用于找到的条目:
          如果条目不在already_found中:
            回调函数(输入)
            已找到.append(条目)
    

    这样做将使用:

    def my_callback(new_entry):
      print new_entry # 或者更有趣的东西......
    
    
    查找蓝牙(我的回调)
    

Two interfaces would be useful.

  1. scan() itself, which returned a list of found devices, such that I could call it to get an instantaneous snapshot of available bluetooth. It might take a max_seconds_to_search or a max_num_to_return parameter.
  2. A "notify on found" function that accepted a callback. For instance (maybe typos, i just wrote this off the cuff).

    def find_bluetooth(callback_func, time_to_search = 5.0):
      already_found = []
      start_time = time.clock()
      while 1: 
        if time.clock()-start_time > 5.0: break
        found = scan()
        for entry in found:
          if entry not in already_found:
            callback_func(entry)
            already_found.append(entry)
    

    which would be used by doing this:

    def my_callback(new_entry):
      print new_entry    # or something more interesting...
    
    
    find_bluetooth(my_callback)
    
如痴如狂 2024-10-05 01:54:42

如果我收到您的问题,您希望将 scan() 放在一个单独的文件中,以便以后可以重复使用。

创建utils.py

def scan():
    # write code for scan here.

创建WiFi.py

import utils

def scan_wifi():
    while True:
        cli = utils.scan()
    ...
    return

If I get your question, you want scan() in a separate file, so that it can be reused later.

Create utils.py

def scan():
    # write code for scan here.

Create WiFi.py

import utils

def scan_wifi():
    while True:
        cli = utils.scan()
    ...
    return
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文