9. (16 points) Let the function $f(x)=\frac{a}{x}-x$. If for any $x \in\left(\frac{1}{4}, 1\right)$, we have $f(x)\left|x-\frac{1}{2}\right| \leqslant 1$, find the range of the real number $a$.
9. When $x=\frac{1}{2}$, the inequality obviously holds.
(1) If $x \in\left(\frac{1}{4}, \frac{1}{2}\right)$, then the inequality is
$a \leqslant x^{2}-1-\frac{1}{2 x-1}$.
Let $g(x)=x^{2}-1-\frac{1}{2 x-1}\left(x \in\left(\frac{1}{4}, \frac{1}{2}\right)\right)$.
Then $g^{\prime}(x)=2 x+\frac{2}{(2 x-1)^{2}}>0$.
Hence, the function $g(x)$ is monotonically increasing on the interval $\left(\frac{1}{4}, \frac{1}{2}\right)$.
Thus, $g(x)>g\left(\frac{1}{4}\right)=\frac{17}{16}$.
Therefore, it is sufficient that $a \leqslant \frac{17}{16}$.
(2) If $x \in\left(\frac{1}{2}, 1\right)$, then the inequality is
$a \leqslant x^{2}+1+\frac{1}{2 x-1}$.
Let $h(x)=x^{2}+1+\frac{1}{2 x-1}\left(x \in\left(\frac{1}{2}, 1\right)\right)$.
Then $h^{\prime}(x)=2 x-\frac{2}{(2 x-1)^{2}}$
$$
=\frac{2\left(4 x^{2}+1\right)(x-1)}{(2 x-1)^{2}}h(1)=3$.
Therefore, it is sufficient that $a \leqslant 3$.
In summary, the range of real number $a$ is $\left(-\infty, \frac{17}{16}\right)$.