Linux Windows 上的 fcntl 替代品

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1422368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:42:01  来源:igfitidea点击:

fcntl substitute on Windows

pythonwindowslinux

提问by Ram Rachum

I received a Python project (which happens to be a Django project, if that matters,) that uses the fcntlmodule from the standard library, which seems to be available only on Linux. When I try to run it on my Windows machine, it stops with an ImportError, because this module does not exist here.

我收到了一个 Python 项目(它恰好是一个 Django 项目,如果这很重要的话),它使用fcntl标准库中的模块,它似乎只在 Linux 上可用。当我尝试在我的 Windows 机器上运行它时,它以 停止ImportError,因为这里不存在这个模块。

Is there any way for me to make a small change in the program to make it work on Windows?

有什么方法可以让我对程序进行一些小的更改以使其在 Windows 上运行吗?

采纳答案by nosklo

The substitute of fcntlon windows are win32apicalls. The usage is completely different. It is not some switch you can just flip.

fcntlWindows 上的替代品是win32api调用。用法完全不同。它不是您可以翻转的开关。

In other words, porting a fcntl-heavy-user module to windows is not trivial. It requires you to analyze what exactly each fcntlcall does and then find the equivalent win32apicode, if any.

换句话说,将fcntl-heavy-user 模块移植到 Windows 并非易事。它要求您分析每个fcntl调用究竟做了什么,然后找到等效的win32api代码(如果有)。

There's also the possibility that some code using fcntlhas no windows equivalent, which would require you to change the module api and maybe the structure/paradigm of the program using the module you're porting.

也有可能使用的某些代码fcntl没有 Windows 等效项,这将需要您更改模块 api 以及使用您正在移植的模块的程序的结构/范式。

If you provide more details about the fcntlcalls people can find windows equivalents.

如果您提供有关fcntl调用的更多详细信息,人们可以找到 Windows 等效项。

回答by Joeri

Although this does not help you right away, there is an alternative that can work with both Unix (fcntl) and Windows (win32 api calls), called: portalocker

尽管这不能立即帮助您,但有一种替代方法可以同时用于 Unix (fcntl) 和 Windows(win32 api 调用),称为:portalocker

It describes itself as a cross-platform (posix/nt) API for flock-style file locking for Python. It basically maps fcntl to win32 api calls.

它将自己描述为一个跨平台 (posix/nt) API,用于 Python 的 flock-style 文件锁定。它基本上将 fcntl 映射到 win32 api 调用。

The original code at http://code.activestate.com/recipes/65203/can now be installed as a separate package - https://pypi.python.org/pypi/portalocker

http://code.activestate.com/recipes/65203/ 上的原始代码现在可以作为单独的包安装 - https://pypi.python.org/pypi/portalocker

回答by Muhammad Soliman

The fcntl module is just used for locking the pinning file, so assuming you don't try multiple access, this can be an acceptable workaround. Place this module in your sys.path, and it should just work as the official fcntl module.

fcntl 模块仅用于锁定固定文件,因此假设您不尝试多次访问,这可能是一个可接受的解决方法。将此模块放在您的 中sys.path,它应该可以作为官方 fcntl 模块工作。

Try using this modulefor development/testing purposes only in windows.

尝试仅在 Windows 中将此模块用于开发/测试目的。

def fcntl(fd, op, arg=0):
    return 0

def ioctl(fd, op, arg=0, mutable_flag=True):
    if mutable_flag:
        return 0
    else:
        return ""

def flock(fd, op):
    return

def lockf(fd, operation, length=0, start=0, whence=0):
    return