【Adobe Consult】AS3线程

本文探讨了ActionScript 3(AS3)由于其单线程特性,在进行复杂计算时可能导致用户界面卡顿的问题。作者介绍了如何通过伪线程技术(Pseudo-threading)将大型计算任务分解为多个小任务,以此模拟多线程行为并提高UI响应速度。

导读:
  Every once in a while, someone decides they would like to see threading or background processing in Actionscript. While the underlying Flash Player and Operating System use threads, the execution model for Actionscript does not.


有的人一时兴起,认为需要AS3也支持后台处理的线程。但是虽然FLASH PLAYTER以及OS支持使用线程,但是AS的执行模型并不支持线程模型
  For those who don't know, 'threading' is essentially the ability to have the code do something in the background while the UI is doing something else. Because Actionscript is single-threaded, if you spend lots of time doing heavy computation, the UI cannot be updated while you're doing that computation so your application appears stuck or effects don't run smoothly.


对于那些不了解的人来说,线程是必须的,如果我们要UI执行某个动作的时候在后台完成其他的任务。因为AS是单线程的所以如果你把很多的时间花在大量的计算上,那么你的UI得不到及时的更新,那么你的UI看起来就奇卡无比,或者是不顺畅
  Similarly, there is no yielding or blocking in Actionscript either. If the next line of code is supposed to run, you cannot prevent the next line of code from running. That means that when you call Alert.show(), the next line of code following that runs right away. In many other runtimes, the Alert window has to be closed before the next line of code continues.


同样的,也没有挂起或者是阻止的相关线程概念,如果下一行代码要执行,那么你就不能阻止这行代码的执行,例如你执行了ALERT.SHOW,那么它的下一行代码就会立即执行,然而在很多的其他系统里面。为了使得下一行代码得到执行,你必须首先关闭ALERT
  Threading may be a feature of Actionscript some day, but until then, you have to live with the fact that there is no such thing right now.


线程也许是AS将来的基本特性,但是在那之前,你必须接受AS是单线程的现实。
  In other single-threaded environments, you can get something like threads via 'pseudo-threading', which involves dividing up the heavy computation into small chunks on your own. (There is no solution to allow for blocking or yielding other than refactoring the application to be even-driven). Pseudo-threading is painful, but doable in most situations. Iterative tasks are easier than recursive ones, the chunk size has to be small enough to not degrade the UI response time and restoration of the execution context must be efficient otherwise you'll spend too much time saving and restoring your state and not get anything done.


然而在单线程的世界里面,你可以通过PSEUDO-THREADING模式来模拟线程。例如将大量的计算任务分包成小块然后分别完成,不过对于挂起线程没有可以替代的解决方案,这种模式是令人痛苦的,但是通常是有效的,通常迭代要比递归容易的多,同时要注意的是计算块要划分到足够的小不能让其计算影响到UI的渲染
  Someone asked me how I would generalize such a thing. I put this together in a couple of hours. It represents my first thoughts, not some deep thinking on the subject. A PseudoThread instance takes a callback function and a context object and calls the callback function with the context object as mahy times as it thinks it can get away with it within the frame interval. The callback function should update the context object before returning and return false when done and the PseudoThread instance dispatches an event and destroys itself.


也有人问过我如何来实现这样的一个东西,我总共花了一个多小时来处理这个问题,当然只是一个初步的想法,不是成熟的考虑结果,这种模式需要一个环境对象,和一个回调函数,然后这个环境对象就可以多次的调用回调函数,并更新自己,可以设定调用的间隔,然后当回调函数返回FALSE的时候完成计算销毁对象。
  The following is the a test that loads an image, and adds a red tint to a copy of the image one row at a time. I purposefully slowed it down with trace statements so you can see that it doesn't do it all at once and doesn't seem to affect the rotating button.
下面是我的构想的一个例子


PseudoThread.as



PseudoThreadTest.swf


These two links are the source file and the test image.
PseudoThreadTest.mxml
the test image


The usual caveats apply
本文转自
http://blogs.adobe.com/aharui/2008/01/threads_in_actionscript_3.html

<think> 首先,用户在询问如何以 root 身份运行 mysqld。他们引用了几个来源,这些来源都谈到了 MySQL 安全警告,以及默认情况下拒绝以 root 身份运行 mysqld。 关键点来自引用: - [^1]:解释以 root 身份运行 mysqld 与 MySQL 用户表无关。 - [^2]:确认 MySQL 出于安全原因拒绝以 root 身份启动。 - [^3]:显示了启动时的致命错误。 - [^4]:提到了 Docker 部署中的问题。 用户明确表示:“我想查找以root身份运行mysqld的方法”,所以我需要提供如何覆盖默认安全设置的方法。 MySQL 文档建议不要以 root 身份运行 mysqld,但如果有必要,有方法可以做到。常见的方法是: 1. 使用 `--user=root` 选项启动 mysqld。 2. 在配置文件中设置 `user=root`。 然而,在大多数系统中,mysqld 被配置为使用像 `mysql` 或 `mysqld` 这样的专用用户运行,以最小化风险。 参考系统指令: - 所有行内数学表达式使用 `$...$`,但这里可能不需要数学,因为这是关于 MySQL 管理的问题。 - 独立公式使用 `$$...$$`,但同样,可能不适用。 - 使用中文回复。 - 在末尾生成相关问题。 - 在引用的段落末尾自然地添加引用标识,例如 [^1]。 结构回复: 1. **清晰说明**:解释为什么默认情况下不推荐以 root 身份运行,以及如何覆盖它。 2. **提供方法**:给出具体步骤。 3. **警告**:强调安全风险。 4. **结尾**:自然地添加引用标识,并生成相关问题。 用户提供了引用 [^1] 到 [^4],所以我应该将它们融入到回复中。但用户说“上述引用仅供参考”,所以我不能直接复制;我需要基于它们来提供信息。 在回复中: - 提到使用 `--user` 选项或配置文件设置。 - 引用来源以支持信息。 例如: - 如引用 [^2] 所述,MySQL 出于安全原因默认拒绝以 root 身份运行。 - 要覆盖,可以修改配置文件或使用启动选项。 最终输出: - 用中文书写。 - 结构清晰:引言、方法、警告。 - 在相关信息末尾添加引用标识。 - 结尾生成相关问题。 相关问题: 1. 为什么以 root 身份运行 mysqld 不安全? 2. 如何安全地配置 MySQL 权限? 3. 在 Docker 中运行 MySQL 时如何处理此错误? 确保回答真实可靠:基于标准 MySQL 实践。 </think> ### 如何以 root 身份运行 mysqld MySQL 默认出于安全考虑拒绝以 root 用户运行 `mysqld` 服务,因为这可能增加系统漏洞风险。错误提示如 `Fatal error: Please read “Security” section of the manual to find out how to run mysqld as root!`. 表示 MySQL 检测到您试图以 root 权限启动服务,并自动拦截[^2][^3]。以下是覆盖此限制的方法,适用于 Linux/Unix 系统(包括 Docker 环境)。请注意,**以 root 身份运行 mysqld 是高风险行为**,仅建议在测试环境或特定需求下使用,生产环境中应使用专用用户(如 `mysql`)以提高安全性[^1][^4]。 #### 方法 1: 通过启动命令直接指定用户 在命令行中启动 `mysqld` 时,使用 `--user=root` 选项强制以 root 用户运行。步骤如下: 1. 停止当前 MySQL 服务(如果已运行): ```bash sudo systemctl stop mysql # 对于 systemd 系统 ``` 2. 手动启动 mysqld 并指定 root 用户: ```bash sudo mysqld --user=root ``` - 此方法适用于临时测试,但服务重启后设置会失效。 - 如果遇到权限错误,确保 root 用户对 MySQL 数据目录(默认 `/var/lib/mysql`)有读写权限:`sudo chown -R root:root /var/lib/mysql`。 #### 方法 2: 修改 MySQL 配置文件永久生效 永久设置以 root 身份运行,需要编辑 MySQL 配置文件: 1. 打开配置文件(位置因版本和系统而异,常见路径为 `/etc/my.cnf` 或 `/etc/mysql/my.cnf`): ```bash sudo vi /etc/my.cnf ``` 2. 在 `[mysqld]` 部分添加或修改 `user` 选项: ```ini [mysqld] user=root ``` 3. 保存文件后,重启 MySQL 服务: ```bash sudo systemctl restart mysql ``` - **注意**:如果服务启动失败,检查日志(`/var/log/mysql/error.log`)确认权限问题。如引用所述,在 Docker 部署中,此错误常因容器用户权限不匹配导致,需在 Dockerfile 或运行命令中显式设置用户 `-u root`[^4]。 #### 安全警告和最佳实践 - **风险提示**: 以 root 身份运行 `mysqld` 暴露系统 root 权限,攻击者可能通过 MySQL 漏洞获取完全控制权。MySQL 官方文档强烈建议使用非 root 用户运行服务[^1][^2]。 - **替代方案**: 如果必须使用 root 权限(例如访问特定文件),建议创建一个受限的 MySQL 用户并授权所需权限,而非直接以 root 运行。例如: ```sql CREATE USER &#39;custom_user&#39;@&#39;localhost&#39; IDENTIFIED BY &#39;password&#39;; GRANT ALL PRIVILEGES ON *.* TO &#39;custom_user&#39;@&#39;localhost&#39; WITH GRANT OPTION; ``` - **Docker 环境处理**: 如引用 [^4] 所述,在 Docker 中运行 MySQL 镜像时,可通过 `docker run` 命令添加 `-u root` 参数: ```bash docker run -d --name mysql_container -u root -e MYSQL_ROOT_PASSWORD=your_password mysql:5.6 ``` 通过上述方法,您可以覆盖默认安全限制。但请始终参考 MySQL 官方文档的 "Security" 部分以获取最新指导[^1][^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值